How to extract data from diary?

14 views (last 30 days)
Sam
Sam on 19 Jun 2020
Answered: Rik on 19 Jun 2020
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 Comments
Rik
Rik on 19 Jun 2020
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 on 19 Jun 2020
Edited: Sam on 19 Jun 2020
Hello,
Thank you Rik. I would highly appreciate a little more help. I've attached this .txt file now.
Thanks!

Sign in to comment.

Accepted Answer

Rik
Rik on 19 Jun 2020
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

More Answers (1)

Jyotirmay Mishra
Jyotirmay Mishra on 19 Jun 2020
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

Tags

Community Treasure Hunt

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

Start Hunting!