Errors using importing a matrix using textscan
3 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Ken Atwell
on 3 Jul 2013
The presence of multiple spaces between your numbers might be the culprit. Try
c=textscan(fid, ['%d', '%f', '%f', '%f'], 'MultipleDelimsAsOne', true, 'Delimiter', ' ')
3 Comments
Ken Atwell
on 4 Jul 2013
What if you, after the textscan, did:
x = c{2};
y = c{3};
z = c{4};
More Answers (1)
See Also
Categories
Find more on Data Import and Export in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!