Why does SSCANF return data that is incorrectly formatted?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have the following array that I am trying to pull numbers out of:
NewArray = ['sdd 46 6 ewd'; 'jkl 7 89 jdw']
I use the following format statement with SSCANF to pull the numbers out:
b = sscanf(NewArray,'%*s %d %d %*s',[2,inf])
I get the result:
b =
476
869
when I would like to get the result:
b =
46 6
7 89
The format statement looks correct but it appears to be reading each row character in a column before going to the next column.
Réponse acceptée
MathWorks Support Team
le 27 Juin 2009
When MATLAB reads a specified file, it attempts to match the data in the file to the format string. If a match occurs, the data is written into the matrix in column order. If a partial match occurs, only the matching data is written to the matrix, and the read operation stops.
This means that the data is read into the resulting matrix in column order. To resolve the above issue, use the following loop:
for i = 1:2
c(i,:)=sscanf(NewArray(i,:)','%*s %d %d %*s',[1,inf])
end
Where the resulting matrix c is:
c 2x2 32 double array
c =
46 6
7 89
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB 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!