I would like to use matlab to add information from a .dat file to existing MatLab variables
3 views (last 30 days)
Show older comments
I have a simulation that outputs a report in the .dat file format. I would like a piece of code that can use two line near the top of the script to determine where the file should be added and saved.
Then I want the code to take all the values and pout them into the correct variable so I can plot the graph as I go. The two lines that determine where the data should be added are as follows
*Mesh Parameters*
Elements average length: 5
Fibres direction elemements factor: 4
The lines with usable data are as follows
Delta: 15
****************
*Average Values*
****************
E11 E22 E33 G12 G23 G13 nu12 nu23 nu13
mean std mean std mean std mean std mean std mean std mean std mean std mean std
3.0214E+05 1.1563E+03 1.0628E+05 8.9269E+02 1.0676E+05 1.4210E+03 8.6614E+04 3.4647E+02 4.5477E+04 5.0063E+02 6.2538E+04 1.5696E+03 1.9722E-01 1.3478E-03 2.5901E-01 3.1920E-03 2.1379E-01 3.0072E-03
ID: 1
E11 E22 E33 G23 G13 G12 nu23 nu13 nu12
3.0326E+05 1.0676E+05 1.0670E+05 4.5890E+04 6.3525E+04 8.6842E+04 2.6055E-01 2.1248E-01 1.9705E-01
I would like to be able to extract the values below ID: 1 and ignore the mean and std values.
Delta: 15
****************
*Average Values*
****************
---
ID: 1
---
ID: 2
---
ID: 3
The second piece of code is repeated multple times in the .dat file as highlighted above.
Thanks in advance
0 Comments
Accepted Answer
Mathieu NOE
on 6 Dec 2022
Edited: Mathieu NOE
on 6 Dec 2022
hello
try this
the text file used for my code is attached
hope it helps
filename = 'text.txt';
D=readlines(filename); % read as string array
%% 1/ get *Mesh Parameters*
tmp = split(D(contains(D,'Elements average length')),':');
Elements_average_length = str2num(tmp(2));
tmp = split(D(contains(D,'Fibres direction elemements factor')),':');
Fibres_direction_elemements_factor = str2num(tmp(2));
%% 2/ get data
% first block of 4 lines
idx = find(contains(D,'Delta:'))
L4 = D(idx:idx+3);
% blocks of ID data (labels, values)
idx = find(contains(D,'ID:'))
Ldata = D(idx(1):idx(end)+2);
% export to text file
out = [L4;Ldata];
writematrix(out,'out.txt');
% for information / use ?
for ci =1:numel(idx)
labels{ci} = split(D(idx+1));
values{ci} = str2double(split(D(idx+2)));
end
More Answers (0)
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!