필터 지우기
필터 지우기

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 CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by