Errors using importing a matrix using textscan
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Ken Atwell
le 3 Juil 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 commentaires
Ken Atwell
le 4 Juil 2013
What if you, after the textscan, did:
x = c{2};
y = c{3};
z = c{4};
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Data Import and Export dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!