How to pass textscan data to array?
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2022-9-10
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));
采纳的回答
dpb
2022-9-10
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 个评论
dpb
2022-9-10
编辑:dpb
2022-9-10
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!