How to pass textscan data to array?
古いコメントを表示
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 件のコメント
Star Strider
2022 年 9 月 10 日
I am not certain what you are doing.
The textscan function produces a cell array as output, so it will be necessary to use the cell2mat function to create a matrix from it (which should be possible since all the data in ‘startAquisition’ appear to be numeric). It might be helpful to include the name-value pair 'CollectOutput',true in the textscan call.
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));
Biza Ferreira
2022 年 9 月 10 日
編集済み: Biza Ferreira
2022 年 9 月 10 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Language Support についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!