How to pass textscan data to array?

9 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
dpb
dpb on 10 Sep 2022
As usual, it would be much easier to provide useful code specific to the situation if you would attach the input file (if it's large, just the header and a few representative data lines would suffice) as a .txt file (use the paperclip icon).
As a couple comments on above code,
%OPEN FULL PATH FILE
fileID = fopen(fullfile( FilePath, FileName),"r");
startHeader = textscan(fileID ,'%s', 'WhiteSpace', '\n'); %START TEXTSCAN HEADER
Header = startHeader{1}; %CONVERT INT 1X1 MATRIX
Header = Header(strncmp(Header, '#', 1)); %END TEXTSCAN HEADER
headerLine = numel(Header); %COUNT HEATHERLINES
totalLines = numel(startHeader{1})-headerLine; %GET TOTAL OF FILE ROWS EXCLUDE HEADER LINES
for k = 1:headerLine %PASSING HEADER VARIABLES TO FIELD
label{k} = extractBetween(Header{k},'#',':'); %LABEL
field{k} = extractAfter(Header{k},':'); %FIELD
end
%fclose(fileID); %CLOSE THE INSTANCE TO READ THE HEADER
%fileID = fopen(fullfile( FilePath, FileName),"r"); %OPEN AGAIN THE FILE
% don't close file just to reopen it immediately, use
frewind(fileID)
% instead...
j=0;
% if you correctly read file, should know how many lines, don't use "magic"
% numbers in code -- this looks like sonmething must have gone wrong...
for j = 1:12%totalLines
startAquisition(j,:) = textscan(fileID, '%d %f %f %f %f %f %f %f %f %f %f', ...
'HeaderLines',headerLine,'Whitespace','\n')
end
The use of 'HeaderLines',headerLine in each call in a loop such as the above is going to skip every other record and the use of 'Whitespae','\n' is peculiar here...
Without the specific file structure itself to see we can't really tell for absolute certain, but I'd wager you could replace all of the above with a single call to
data=readmatrix(fullfile( FilePath, FileName));
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
dpb
dpb on 10 Sep 2022
Edited: dpb on 10 Sep 2022
You don't need a loop to do that in MATLAB. The header of your above file says the sample rate is 1000, one presumes it's Hz although that isn't given.
You parsed the header; one would presume for precisely such reason -- if not going to use it, why bother?
ix=matches(label,'SamplingFrequency'); % which is the sampling rate?
Fs=str2double(field{ix}); % the sampling frequency value
dt=1/Fs; % the sample time delta
dataAcquisition=dt*[0:size(dataAcquisition,1)-1].'; % append time vector as new column
Or, you might consider turning it into a timetable; there are many useful builtin features there depending on what next you want to do.
And, of course, you could have gone ahead and done the numeric conversion when you were parsing the rest of the header. A clever use of regexp could return the tokens directly.
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)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!