0
votes

I am trying to import numerical data as well as ASCI info that was recently tacked on to the file. I can import the numerical data into a data structure just fine, but MATLAB(R2014a)'s importdata function does not even recognize the written (event) data.

I've looked up questions regarding this issue from a couple years ago but they haven't really been answered. As per those suggestions, I've tried: data_temp = cell2mat(textscan(fopen('P:\FileName))) which brings in an empty matrix, and the 'load' function versus 'importdata'. The load function fails because my data have a different number of columns per row.

So does anyone know a function/work-around to bring in the last few rows of the data?

{I don't want to use the ImportWizard because that requires hand-selection and I am writing a user-friendly code that enters data files via GUI}

Here is an example data file - has rows 1:412 of numerical data and rows 413:437 of event data:

409 0.000000 0.000000 0.000000 0.000000

410 0.000000 0.000000 0.000000 0.000000

411 0.000000 0.000000 0.000000 0.000000

412 0.000000 0.000000 0.000000 0.000000

Event: LHS
Number of Frames: 2
Frame 1: 222 Frame 2: 327 Event: LTO
Number of Frames: 2
Frame 1: 181 Frame 2: 283 Event: RHS
Number of Frames: 2
Frame 1: 169 Frame 2: 274 Event: RTO
Number of Frames: 1
Frame 1: 232 Event: LHS FP
Number of Frames: 1
Frame 1: 222 Event: RHS FP
Number of Frames: 1
Frame 1: 169

Thanks for your time and help!

2

2 Answers

1
votes

I'm not sure your ultimate goal, as this is not a pure numeric file, i.e., some numbers are mixed within lines in the event blocks. Thus I'm not sure whether you want to extract numbers within the lines or you're just asking how to read all the lines and store in one variable, as readlines() in python. I assume what you asked falls into the later one. I guess you can fgets() to read each line and then store in a cell variable. Thus the skeleton of the code will be:

fid = fopen('filename.txt', 'r');
lines = {};
tline = fgets(fid);
while ischar( lines{end} )
    lines{end+1} = tline;
    tline = fgets(fid);
end
fclose(fid);

The final result is stored in 'lines' variable, and you can further parse them if needed.

Ref. http://www.mathworks.com/help/matlab/ref/fgets.html

1
votes

You can import a .data file via MATLAB's import function 'importdata' but it doesn't import text that are not headers. This code opens and scans the file line by line searching for the text 'Event'. (I can't take credit for this code- my friend wrote it for me, but I wanted to post it for someone trying to do the same thing)

fn = ('file.data');

B = importdata(fn);
fid = fopen(fn);
tline = fgets(fid);
D=[];
while ischar(tline)  % while not at the end of file
    if strncmp(tline,'Event',5) % If the first 5 letters are 'Event'
        Evt=strtrim(strsplit(tline,'\t')); % find event name
        Evt=Evt{2};

    Nb = fgets(fid); % Get the number of frames
    Nb=sscanf(Nb,'  Number of Frames:   %d');
   F=[];
    for f=1:Nb % For each frame, get the frame number
        ln = fgets(fid);
        F(f)=sscanf(ln,['       Frame ' num2str(f) ':   %d']);
    end 
    D(end+1).Name   = Evt;
    D(end).Frames = F;
end
tline = fgets(fid);
end
fclose(fid);