dlmread reads rows instead of columns

4 views (last 30 days)
Alexey Pustovarenko
Alexey Pustovarenko on 11 Dec 2017
Answered: Yaakov Shaked on 25 Mar 2019
Hi all,
I have tried to import a column from a .txt file (contains 976 rows and 5 columns of numbers with a tab delimiter) using M = dlmread('filename.txt','\t',[0,0,5,0]), so M should contain first six elements of the first column. It gives 1x6 array instead: the first row of the txt file + 1 element from the second row. So it reads rows instead of columns. Am I missing something? How could I solve this problem? Sorry for my English.

Answers (2)

KL
KL on 11 Dec 2017
Edited: KL on 11 Dec 2017
EDITED
something like the following,
dlmread(filename,' ',[0 0 4 0])
or use textscan,
filename = 'dummy.txt';
noRows = 5;
A = cell(noRows,1);
fid = fopen(filename,'r');
fspec = '%f %*f %*f %*f %*f\n'; % use * to ignore reading
for k =1:noRows
A(k,1) = textscan(fid,fspec,1);
end
fclose(fid);
  2 Comments
Alexey Pustovarenko
Alexey Pustovarenko on 11 Dec 2017
Thanks a lot. It works perfectly. But I'm still wondering why it failed with the dlmread. Is it a bug?
KL
KL on 11 Dec 2017
Not a bug, should be something to do with the file I reckon.
But I'm used to using textscan for custom import as it gives more freedom.

Sign in to comment.


Yaakov Shaked
Yaakov Shaked on 25 Mar 2019
I just ran in to a same problem - the data was being read row by row instead of column by column.
I found out that my delimeter was wrong (a tab insted of a space).
Once I fixed the delimeter, the data was read correctly.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!