Why does SSCANF return data that is incorrectly formatted?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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.
Respuesta aceptada
MathWorks Support Team
el 27 de Jun. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Get Started with MATLAB en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!