read complicated .txt file into Matlab

Zoe picture Zoe · Jun 3, 2011 · Viewed 7.2k times · Source

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!

Answer

gnovice picture gnovice · Jun 3, 2011

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