Why does SSCANF return data that is incorrectly formatted?
1 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
MathWorks Support Team
2009-6-27
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 个评论
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!