Errors using importing a matrix using textscan
2 ビュー (過去 30 日間)
古いコメントを表示
Hi, I am facing some problems when I am writing the following code in Matlab. I have a .txt file with 4 columns and 461 rows (the gap between two columns are 3/4 spaces).
Please see the program below and suggest a solution.
id=fopen('Node_kheya.txt', 'r'); c=textscan(fid, ['%d', '%f', '%f', '%f']); a=horzcat(c{:}); fclose(fid);
%the second column of a is taken as the primary x co-ordinates %and the third column as y co-ordinates. fourth as z
xp=a(:,2); yp=a(:,3); zp=a(:,4);
% transfer from array to vector
x=reshape(xp,1,461); y=reshape(yp,1,461); z=reshape(zp,1,461);
% add the first element of each vector to the last position
s=x(1,1); t=y(1,1); q=z(1,1);
X=[x s]; Y=[y t]; Z=[z q];
But when I write c in the command prompt, it shows:
Columns 1 through 3
[461x1 int32] [461x1 double] [461x1 double]
Column 4
[461x1 double]
When I write a, it shows a matrix with the correct dimensions, but all the elements in the column 2,3and 4 are 0.
0 件のコメント
採用された回答
Ken Atwell
2013 年 7 月 3 日
The presence of multiple spaces between your numbers might be the culprit. Try
c=textscan(fid, ['%d', '%f', '%f', '%f'], 'MultipleDelimsAsOne', true, 'Delimiter', ' ')
3 件のコメント
Ken Atwell
2013 年 7 月 4 日
What if you, after the textscan, did:
x = c{2};
y = c{3};
z = c{4};
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Data Import and Export についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!