How to extract data from diary?
14 次查看(过去 30 天)
显示 更早的评论
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
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.
采纳的回答
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.
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
0 个评论
更多回答(1 个)
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
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!