How to read strings from file with fscanf or sscanf (NOT textscan)?
Show older comments
So, of course, I'm having a little trouble right now. I'm trying to read a text file that goes something like this in a columnar order. What I would like to do is store the number, character and string columns seperately in arrays.
[Numbers] [Characters] [Strings]
Now, while I have figured out how to read the number and character columns into their own arrays, I cannot seem to do so with the string column. At least, not with fscanf or sscanf, which are the commands I want to use.
How can you read a file organized as such using fscanf or sscanf? (I know about textscan, I want to know if this is possible with fscanf or sscanf).
The first thing I tried was the following:
fid = fopen('Data.txt', 'w+');
B = fscanf(fid, '%d %c %s', [3,inf]);
Now while this worked fine for just the numbers and chars (i.e. B = fscanf(fid, '%d %c', [2,inf])), it fails for the above in the sense that it reads everything out of order (e.g. instead of B = [1,2,3...; a,b,c...; ABC, DEF, GHI...] I get B = [1,65,65; 66, 67, 2;...], just junk basically).
So I researched a bunch and tried out this:
fid = fopen('Data.txt', 'w+');
i = 1;
while ~feof(fid)
line = fgets(fid);
M(i) = sscanf(line, '%d, %c, %s', [3,inf];
i = i+1;
end
This runs, but M ends up coming out only as a row vector consisting of the first column of numbers in the data file. It just completely ignores the existence of chars and strings.
Now, to get a better understanding of the sscanf function I tried the following
fid = fopen('Data.txt', 'w+');
i = 1;
while ~feof(fid)
line = fgets(fid);
M(i) = sscanf(line, '%d, %d, %d', [3,inf];
i = i+1;
end
For a sample set of data consisting of just columns of numbers. This, incidentally, does exactly the same thing as previously; it just reads the first number column of the data and quits. So, I don't even know how to use sscanf, feof, or fgets properly, basically. So I could also use some help here as well.
And I know trying to read just columns of numbers is trivial with fscanf, but I'm trying to understand sscanf and fgets here.
3 Comments
per isakson
on 17 May 2013
"just junk basically" or is it ascii code?
>> double('ABC')
ans =
65 66 67
Could you provide us with a few lines of your data file? There are several options that we can discuss.. meanwhile, be aware that 65 is the ASCII code of character 'A', 66 for 'B', etc, so what you thought was junk is not; it is 'ABC' not read/interpreted/displayed as a string.
LifeSux SuperHard
on 17 May 2013
Edited: LifeSux SuperHard
on 17 May 2013
Accepted Answer
More Answers (1)
per isakson
on 17 May 2013
Edited: per isakson
on 17 May 2013
What you see is as documented. Clip from on-line help:
sscanf finds three word matches for %s and two numeric matches for %d. Because
the format specifier has a mixed %d and %s format, sscanf converts all
nonnumeric characters to numeric:
[str count] = sscanf('5 strings and 4 spaces', '%d%s%s%d%s');
str'
Columns 1 through 9
5 115 116 114 105 110 103 115 97
Columns 10 through 18
110 100 4 115 112 97 99 101 115
count
count =
5
sscanf returns a numeric or a character array.
textscan can produce the output you are looking for.
4 Comments
LifeSux SuperHard
on 17 May 2013
per isakson
on 17 May 2013
Your comment, "just junk basically" , gave me the wrong impression, sorry!
LifeSux SuperHard
on 17 May 2013
per isakson
on 18 May 2013
Edited: per isakson
on 18 May 2013
"the sscanf problem" does that refer to "everything out of order"? I assume so.
The source of the "problem" is that Matlab reads data in column order, which is because of early influences from FORTRAN.
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!