complicated formatted text with textscan

조회 수: 3 (최근 30일)
Jules Ray
Jules Ray 2020년 2월 14일
댓글: Jules Ray 2021년 8월 3일
Dear collegues, I am complicated reading a .txt with a peculiar format. This .txt file is an output of an R function, which I am running with vanilla inside matlab.... So i need to load this txt file programatically.
This is a part of the contents of the text file :
Reservoir_age_offset_14C_yrs_ranges at 90% confidence intervals
Identification_number: 1
MinRange MaxRange probability
-3962 -1741 90
Mode MidRange Median
-2709 -2852 -2761
Identification_number: 2
MinRange MaxRange probability
-9770 -8066 90
Mode MidRange Median
-8898 -8918 -8913
Identification_number: 3
MinRange MaxRange probability
-3762 4226 87.6
4335 4699 2.4
Mode MidRange Median
-559 468 305
Identification_number: 4
MinRange MaxRange probability
-401 2224 90
Mode MidRange Median
593 911 838
Identification_number: 5
MinRange MaxRange probability
-709 976 90
Mode MidRange Median
307 134 262
etc etc etc .................................................
So, to read this text format I implemented this script, the idea is to reconstruct a matrix from the numerical values in the text file
clear
here=pwd;
filename= 'output.txt';%ths is the text file
fileID2 = fopen(filename);
k=1;
Vale=[];
for j=1:50
s=cell2mat(textscan(fileID2,'%f',3,'headerlines',j,'collectoutput',1));
if isempty(s)==0 %rescue only valid solutions
Vale(:,k)=s; %brrrrrrppp!!
k=k+1;
end
end
fclose(fileID2);
The problem is that I cant read all the numerical values to reconstruct,
it works when I change the number of headerlines manually
e.g.
%s1=cell2mat(textscan(fileID2,'%f',3,'headerlines',7,'collectoutput',1))
However the loop provided incomplete nuber of values
Any idea or more elegant solution with this issue is highly appreciated
Thanks in advance
Jules
  댓글 수: 2
Stephen23
Stephen23 2021년 8월 3일
편집: Stephen23 2021년 8월 3일
Is the block of data for Identification_number: 3 really supposed to have two rows of numeric values?:
This makes the block data format irregular, and likely difficult to parse and store.
Otherwise this task can be achieved quite easily using TEXTSCAN (see attached files).
Jules Ray
Jules Ray 2021년 8월 3일
Thank you Stephen, with textscan is quite more easy... I would like to tag this comment as accepted answer but seems you have to post it below ....
thanks again
best
JR

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2021년 8월 3일
편집: Stephen23 2021년 8월 3일
I removed the extra line of numbers from block 3, the modified data file is attached.
This code imports the modified data file:
opt = {'CollectOutput',true, 'MultipleDelimsAsOne',true, ...
'HeaderLines',1, 'EndOfLine',':', 'Whitespace',' \b\t\n\r'};
fmt = '%f%*s%*s%*s%f%f%f%*s%*s%*s%f%f%f%*s';
fid = fopen('data.txt','rt');
tmp = textscan(fid,fmt,opt{:});
fclose(fid);
mat = tmp{1}
mat = 5×7
1 -3962 -1741 90 -2709 -2852 -2761 2 -9770 -8066 90 -8898 -8918 -8913 3 -3762 4226 87.6 -559 468 305 4 -401 2224 90 593 911 838 5 -709 976 90 307 134 262

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by