janSO
janSO

Reputation: 35

reading in a file with textscan and ignoring certain lines

I have an input file which has rows of integers like this:

1 2 3
4 5 6
7 8 9

I want to read in the file, I have used the textscan function for this kind of task before. But there are a few lines in the data (at random positions) which contain double numbers, for example

<large number of integer lines>
0.12 12.44 65.34
<large number of integer lines>

When reading in the file, I want to ignore these lines. What's the best approach to do this? Can I tell textscan to ignore certain patterns?

Upvotes: 1

Views: 2256

Answers (3)

timgeb
timgeb

Reputation: 78780

Assuming that you don't know the location and number of the lines with floats, and that you don't want lines such as 1.0 2.0 3.0 or 1 2 3.0 my idea would be to read the file line by line and not store lines which contain a . character.

fid = fopen('file.txt');
nums = [];
line = fgetl(fid);
while line ~= -1 % #read until end of file
    if isempty(strfind(line, '.'))
        line = textscan(line, '%d %d %d');
        nums = [nums; line{:}];
    end
    line = fgetl(fid);
end
fclose(fid);

nums is the matrix containing your data.

Upvotes: 1

nkjt
nkjt

Reputation: 7817

One option is to simply just read everything in as floats - use either textscan or if your data is all numeric dlmread or similar might be simpler.

Then just remove the lines you don't want:

data =

    1.0000    2.0000    3.0000
    4.0000    5.0000    6.0000
    0.1200   12.4400   65.3400
    7.0000    8.0000    9.0000

 data(data(:,1)~=round(data(:,1)),:)=[]

data =

     1     2     3
     4     5     6
     7     8     9

If your later code requires that the type of your data matrix is non-float, use uint8 or similar to convert at this point.

Upvotes: 2

Maze
Maze

Reputation: 156

The formatSpec argument could be the one you're searching for: http://www.mathworks.de/de/help/matlab/ref/textscan.html#inputarg_formatSpec

It terminates the reading, if the content does not match the given format. If you call textscan a second time with the same file, it has to start reading where it last terminated.

From linked site:

If you resume a text scan of a file by calling textscan with the same file identifier (fileID), then textscan automatically resumes reading at the point where it terminated the last read.

Upvotes: 4

Related Questions