필터 지우기
필터 지우기

How to extract data from diary?

조회 수: 11 (최근 30일)
Sam
Sam 2020년 6월 19일
답변: Rik 2020년 6월 19일
Hello,
I've used the diary function to create a diary file. When I open the diary file, I've got the following:
normal
1.0e+03 *
1.1797 0.2894 0.0010
1.1797 0.2894 -0.0010
open
1.0e+03 *
1.1797 0.2894 0.0010
1.1797 0.2894 -0.0010
normal
1.0e+03 *
1.2787 0.5115 0.0010
1.2787 0.5115 -0.0010
Now, I want to capture all the numbers that follow after the word 'normal', so I only want the numbers depicted in bold here. How do I read the diary file in Matlab and extract the numbers I want from the diary file?
Thank you.
  댓글 수: 2
Rik
Rik 2020년 6월 19일
The diary file is a plain text file, so you can read it like any other. Then you will have to select the lines that describe the values of normal, and make sure you correctly parse the factor.
If you want more specific help, you can attach the file to your question.
Sam
Sam 2020년 6월 19일
편집: Sam 2020년 6월 19일
Hello,
Thank you Rik. I would highly appreciate a little more help. I've attached this .txt file now.
Thanks!

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

채택된 답변

Rik
Rik 2020년 6월 19일
This would probably be far easier if you captured the output at the source, instead of parsing the diary.
This code works, but is reliant on the point that the data fits on the screen, so you don't have the column indicators.
This uses my readfile function, available on the FEX here.
fileurl='https://www.mathworks.com/matlabcentral/answers/uploaded_files/319027/mydiary2.txt';
data=readfile(fileurl);
counter.out=0;
counter.elem=0;
scalefactor=1;
record=false;
output=cell(0,1);
for n=1:numel(data)
currentline=strtrim(data{n});
if numel(currentline)<1,continue,end%blank line
if isstrprop(currentline(1),'alpha')
%next variable name
scalefactor=1;%reset the scale factor
if strcmp(currentline,'normal')
counter.out=counter.out+1;
counter.elem=0;
record=true;
else
record=false;
end
elseif strcmp(currentline(end),'*')
scalefactor=str2double(currentline(1:(end-1)));
elseif isstrprop(currentline(1),'digit')
if ~record,continue,end
values=cellfun(@str2double,strsplit(currentline));
values=values*scalefactor;
counter.elem=counter.elem+1;
output{counter.out,1}(counter.elem,:)=values;
end
end

추가 답변 (1개)

Jyotirmay Mishra
Jyotirmay Mishra 2020년 6월 19일
Hi,
You can import the diary file as dataset from the import data option in the home tab of MATLAB. Make sure that you select the datatype for the first coloumn as Text or else the text ''normal", "open" will be replaced by NaN.
Now you can use some logic using the a loop and some conditional statements to obtain the data after normal

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by