Matlab: reading text file information with blanks

108 views Asked by At

I have a horrible text file full of blanks (Example below), I need the information in the 6th column (e.i. 9381950, 9332480, and 9997980) but the sporadic blanks are making textread or textscan commands problematic even with detailed formatting. Is there a way to scan each row for the columns [35:41]?

983 8409 hfj  984098    989999999 9381950
688      hfij 786898    775907659 
133 9856                356474764 9332480
95  7409 hfgu 949865    553456546 
914                     343557989 
667 8989                456755688 9997980
2

There are 2 answers

0
Marcin On BEST ANSWER

You can read the file line by line, and since it seems you have fixed format file, you can just extract the columns or characters from each line from predefined place in a line:

out_cell = {};

fid = fopen('data.txt');

tline = fgetl(fid);
while ischar(tline)        
    out_cell{end+1} = tline(20:30);  % put which ever part of line you want      
    tline = fgetl(fid);
end

fclose(fid);

out_cell{:}
0
Aditya On

You could " scan " each row by reading your file using:

fid = fopen('yourfile.extension')
currentLine = fgetl(fid)


# Do some testing here #
# Then proceed to next line using fgetl(fid) again (e.g. using a while loop)

Is this of any help?