Zoe
Zoe

Reputation: 21

read complicated .txt file into Matlab

I would like to read a .txt file into Matlab. One of the columns contains both letters and numbers. (So I guess one way is to read this column is as string.)

The problem is I also need to find out numbers which are larger than 5 within that column.

e.g. The .txt looks like

12 1
21 2
32 7
11 a
03 b
22 4
13 5
31 6

i.e. Ultimately, I would like to get

32 7
31 6

How can I get it?? Any experts, please help!

Upvotes: 2

Views: 7259

Answers (2)

gnovice
gnovice

Reputation: 125854

You can read the contents of the file into a cell array of strings using TEXTSCAN, convert the strings to numeric values using CELLFUN and STR2NUM (characters like 'a' and 'b' will result in the empty matrix []), remove rows of the cell array that have any empty cells in them, then convert the remaining data into an N-by-2 matrix using CELL2MAT:

fid = fopen('junk.txt','r');                        %# Open the file
data = textscan(fid,'%s %s','CollectOutput',true);  %# Read the data as strings
fclose(fid);                                        %# Close the file
data = cellfun(@str2num,data{1},'UniformOutput',false);  %# Convert to numbers
data(any(cellfun('isempty',data),2),:) = [];        %# Remove empty cells
data = cell2mat(data);                              %# Convert to N-by-2 array

The matrix data will now look like this, given your sample file in the question:

>> data

data =

    12     1
    21     2
    32     7
    22     4
    13     5
    31     6

And you can get the rows that have a value greater than 5 in the second column like so:

>> data(data(:,2) > 5,:)

ans =

    32     7
    31     6

Upvotes: 4

Chris A.
Chris A.

Reputation: 6887

fid = fopen('txt.txt','r');  

Aout = []; 

while(1)    
    [a1,count1] = fscanf(fid,'%s',1);
    [a2,count2] = fscanf(fid,'%s',1);
    if(count1 < 1 | count2 < 1)
        break;    
    end
    if(~isempty(str2num(a2)) & str2num(a2) > 5 & (~isempty(str2num(a1))) )
        Aout = [ Aout ; str2num(a1) str2num(a2) ];   
    end
end

fclose(fid);

Violates the unspoken rule of growing a Matlab variable during a loop, but it's text processing anyway so you probably won't notice the slowness.

Edit: Had too many errors in previous version, had to start fresh.

Upvotes: 0

Related Questions