How to pass textscan data to array?

7 views (last 30 days)
Biza Ferreira
Biza Ferreira on 10 Sep 2022
Edited: Biza Ferreira on 11 Sep 2022
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
Biza Ferreira
Biza Ferreira on 10 Sep 2022
Edited: Biza Ferreira on 10 Sep 2022
Hello dpb and star strider,
Thanks for your support, sorry for not send the specific file structure, now I include the INPUT.txt file . My intention is to use the data aquisition to fill a graph dynamically. I have interest in the header too, becouse it have some important information, that is the motive for first part of the code, where I need the total dimention of the file And count the header lines before exclued it. So in the second part i need access to certain columns with intention to charge it in the graph.
Thanks in advance

Sign in to comment.

Accepted Answer

dpb
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
Biza Ferreira
Biza Ferreira on 11 Sep 2022
Edited: Biza Ferreira on 11 Sep 2022
Well that is why i decide mantain the header, which you have mencioned I can rescue all information I need, like the sample frequency, witch can change if the user require... In this moment i am working with a frequency of 125Hz, which give me 0.008s per sample
This way I try to make my code the most adaptative to many situation.
"You don't need a loop to do that in MATLAB. " - That is awesome, becouse loop cause lots of problems in the processing.
"...there are many useful builtin features there depending on what next you want to do." - My next step is try to mount a dynamic graph, where I can analyse the ecg in a timeline. Turn it in a timetable would bring to me some advantages?

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!