フィルターのクリア

dlmread reads rows instead of columns

2 ビュー (過去 30 日間)
Alexey Pustovarenko
Alexey Pustovarenko 2017 年 12 月 11 日
回答済み: Yaakov Shaked 2019 年 3 月 25 日
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.

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by