Clear Filters
Clear Filters

Why does SSCANF return data that is incorrectly formatted?

2 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 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

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!