dlmread reads rows instead of columns

2 次查看(过去 30 天)
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.

回答(2 个)

KL
KL 2017-12-11
编辑:KL 2017-12-11
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 个评论
Alexey Pustovarenko
Alexey Pustovarenko 2017-12-11
Thanks a lot. It works perfectly. But I'm still wondering why it failed with the dlmread. Is it a bug?
KL
KL 2017-12-11
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.

请先登录,再进行评论。


Yaakov Shaked
Yaakov Shaked 2019-3-25
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!

Translated by