Errors using importing a matrix using textscan

3 次查看(过去 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.

采纳的回答

Ken Atwell
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
Ken Atwell 2013-7-4
What if you, after the textscan, did:
x = c{2};
y = c{3};
z = c{4};
Kheya Banerjee
Kheya Banerjee 2013-7-8
thank you for your answer. your solution is ok. actually the problem was reading the text file. the text file was not spaced with tabs or commas. so it was impossible to read through textscan. i have imported the .txt file into exel and now this part of the problem is solved. thank you again for your help, Ken.

请先登录,再进行评论。

更多回答(1 个)

Kheya Banerjee
Kheya Banerjee 2013-7-8
have a 461 by 3 matrix in which the first column is x coordinates, second one y and third one z coordinates. The values of the elements of z column is o. (i.e it is actually 2D data but I need 3D data for further calculation so I kept the third column.). The values of the data set are scattered values.
Now I want to get more coordinate values (x,y,z) within this data set. The z values will be zero.
Can I use the function interp1/2/3??? how??
My main problem is achieving the coordinate matrix with all (prev.+interpolated) values. The interp function shows the value of a desired point, but I do not need that.

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by