Why does SSCANF return data that is incorrectly formatted?

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 27 Giu 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

Più risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by