필터 지우기
필터 지우기

Errors using importing a matrix using textscan

조회 수: 3 (최근 30일)
Kheya Banerjee
Kheya Banerjee 2013년 7월 3일
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