MATLAB Answers

fscanf question when ignoring header

28 views (last 30 days)
m j
m j on 22 Jan 2020
Edited: m j on 23 Jan 2020
Hello,
I have a text file that im having issues with. Ive been able to read my others files but this one for some reason isnt working properly when trying to us fscanf.
The file has 5 headers then data of 2k samples:
SAMPLES: 2000
BITSPERSAMPLE: 32
CHANNELS: 1
SAMPLERATE: 96000
NORMALIZED: FALSE
50
45
30
etc...till 2k values
My code: for convience lets say this file is file # 4.
formatSpec = '%*s %f'; %ignore strings and keep integers
myCell = {}; %create cell to put data into after uigetfile
[file,path] = uigetfile('*.rnd','MultiSelect', 'on');
%creates file{1,X}
%open file # 4
pathname = file{1,4};
%use current pathname to open file
fileID = fopen(pathname,'r');
%read file with formatSpec set to ignore strings and read only numbers
myCell{1,1} = fscanf(fileID,formatSpec);
Pretty straightforward. And my code for opening all of my other files that have different headers/footers works fine,and they all use this same setup for formatSpec and fscanf. But when opening this file it just returns [2000,32,1,96000] in myCell.

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 22 Jan 2020
'%*s %f' does not mean to skip all strings and then look for a number. It means to skip one string and then look for a number. So it will skip 'NORMALIZED' and then try to read a number but will fail because FALSE is not a number.

  3 Comments

m j
m j on 23 Jan 2020
Smh.....Sorry, but thanks! Had misunderstood it,figured it out with:
fileID = fopen(name,'r');
%ignore first 5 lines
for i = 1:5
letsC=fgets(fileID);
end
%read file with formatSpec set to ignore strings and read only numbers
myCell{1,1} = fscanf(fileID,formatSpec);
But is there anyway to remove a header/footer of file without knowing how long the header/footer is within the file? Or is there a way to remove all lines of file that contain strings/chars? And doing this will only leave data....
Just off top of head,maybe go through each line of file and test if line contains a char/string,if it does. fgets it??On cellphone currently so, apologies if this sounds dumb.
Walter Roberson
Walter Roberson on 23 Jan 2020
In the case where the header starts with a known string and ends with a known string then you can use textscan CommentStyle parameter to skip the content.
Otherwise you need to know something more about the pattern that indicates end of header.
A lot of the time it can make sense to read everything in as a single string and then manipulate it with regexp or regexprep.
m j
m j on 23 Jan 2020
Thanks, ill look into making it into and single string a manipulating it. I would just like the freedom to be able to open up multiple files that have different headers/footers and extract the data,regardless if I know how they are set up.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by