0

I have a text file which have 5 columns. Here is sample data:

book   1  3  5  7
paper  3  9  0  2
pen    3  1  2  0
pencil 9  0  3  9

The first column contains character and the other columns are just number. The file contains several rows. I am trying to read that .txt as follows;

fileID = fopen('filename.txt');
C = textscan(fileID,'%s %n %n %n %n');
fclose(fileID);
celldisp(C)

It read the column correctly. However, it reads only the first row, and not all. Why it is happening that way? How to do if I want to read all rows and all columns. Thanks

mee mee
  • 547
  • 3
  • 8
  • 20
  • 1
    you should consider using `dlmread` or `importdata`, `textscan` is the wrong choice, And a part from that `%n` is no [**vakid syntax**](http://www.mathworks.com/help/matlab/matlab_prog/formatting-strings.html). – Robert Seifert Feb 11 '16 at 07:33
  • @thewaywewalk It appears that `%n` can be used in the format spec of [`textscan`](http://mathworks.com/help/matlab/ref/textscan.html). Still, using `%f` is probably more readable. – hbaderts Feb 11 '16 at 07:38
  • I can't reproduce your problem, and since you didn't supply any example data, I'm voting to close the question. – dasdingonesin Feb 11 '16 at 07:45
  • hi thanks for advise. @dasdinogonesin, here is my sample data: 1st row: book 1 3 5 7 , 2nd row: paper 3 9 0 2, 3rd row: pen 3 1 2 0, 4th row: pencil 9 0 3 9. – mee mee Feb 11 '16 at 08:10
  • 1
    @meemee, please edit your original post to include your sample data. Comments are not the right place to add these information. – Hoki Feb 11 '16 at 08:54
  • I manually created a text file and tested your code and it works correctly. Which codification are you using for the file? It might be a problem with the `line feed` and `carriage return` not recognized properly. –  Feb 11 '16 at 09:00
  • Tried ANSI, UTF-8 with and without BOM, with CR, LF and CR+LF. Worked every time. – dasdingonesin Feb 11 '16 at 09:27

1 Answers1

2

I assume you want to create a cell array in which every cell contains a single element from your text file.
The code you provided so far is correct:

fileID = fopen('filename.txt');
C = textscan(fileID,'%s %n %n %n %n');
fclose(fileID);

However now each cell in C contains an entire column from your txt file: enter image description here
where C{1,1} contains the first column, that are four strings (book, paper, pen, pencil).

Now it is time un "unwrap" such cells using this piece of code:

for i=1:size(C,2)
    if(iscell(C{:,i}))
        A(:,i)=reshape(C{:,i},length(C{:,i}),1);
    else
        A(:,i)=num2cell(reshape(C{:,i},length(C{:,i}),1));
    end
end

that basically says "if the cell contains a cell (see C{1,1}) just unwrap its content in length(C{:,i}) different cells. As instead if the cell contains an array, assign each element of this array to a different cell.".
Now the cell array A has the form

enter image description here

and I hope this is what you're looking for.

Community
  • 1
  • 1
AlessioX
  • 3,167
  • 6
  • 24
  • 40