How to pass textscan data to array?
6 views (last 30 days)
Show older comments
Hello fellows,
I have a write a code where I acess to a *.txt file and after get the information from the header, i close with fclose the open file.
After that I call again the open file, and the textscan to get the data without the header file, now I get blocked because I need acess to certain filds in the whole *.txt file to load a dinamic graph, can someone help me
Thanks in advanced
%OPEN FULL PATH FILE
fileID = fopen(fullfile( FilePath, FileName),"r");
%START TEXTSCAN HEADER
startHeader = textscan(fileID ,'%s', 'WhiteSpace', '\n');
%CONVERT INT 1X1 MATRIX
Header = startHeader{1};
%END TEXTSCAN HEADER
Header = Header(strncmp(Header, '#', 1));
%COUNT HEATHERLINES
headerLine = numel(Header);
%GET TOTAL OF FILE ROWS EXCLUDE HEADER LINES
totalLines = numel(startHeader{1})-headerLine;
%PASSING HEADER VARIABLES TO FIELD
for k = 1:headerLine
%LABEL
label{k} = extractBetween(Header{k},'#',':');
%FIELD
field{k} = extractAfter(Header{k},':');
end
%LOAD LABELS AND FIELDS REQUIRED BY POSITION
%
%CLOSE THE INSTANCE TO READ THE HEADER
fclose(fileID);
%OPEN AGAIN THE FILE
fileID = fopen(fullfile( FilePath, FileName),"r");
%CREATE A INITIAL VARIABLE
j=0;
%MAKE A NEW SCAN EXCLUDIN THE HEADER LINES
%PRE-CONSTRUCT A TABLE TO INTRODUCE SAMPLE_COUNT AND TIME
for j = 1:12%totalLines
%samples(j) = sscanf(Header{j}, 'Samples: %f');
startAquisition(j,:) = textscan(fileID, '%d %f %f %f %f %f %f %f %f %f %f', ...
'HeaderLines',headerLine,'Whitespace','\n')
end
fclose(fileID);
3 Comments
Accepted Answer
dpb
on 10 Sep 2022
OK, with the multiple-line header, and parsing it, the code isn't so bad, after all... :)
I'd probably still do a little different, but your first part suffices to locate the header and parse it -- but then, if you're going to close the file and reopen it anyway, then use the information you found before and read the rest as
...
dataAcquisition=readmatrix(fullfile(FilePath,FileName),'NumHeaderLines',headerLine);
and you've got the whole data array in memory -- you can then just remove the columns don't care about by something like
ixKeep=[1 3:6]; % an arbitrary set of column indices wanted; salt to suit...
dataAcquisition=dataAcquisition(:,ixKeep); % and keep those...
3 Comments
More Answers (0)
See Also
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!