Save data during matlab loop
Ältere Kommentare anzeigen
Hello community!
Time for another question :)
My current project is a simulation run by matlab. So mainly it is a call of a "SimulateStep" function in which a lot of different things happen.
One of the things is the call of a huge mex function which gives me some internal data. So, during each call I have a relativly big struct that has a lot of data in it.
And my question is now: What is the most efficient way to save this data during each step and save it with the propriate time? Also, it should be easy to access and read it afterwards.
My current attemp is looking like that:
function saveData(this, time)
% input data
namelist = fieldnames(this.structToSave);
for it = 1: length(namelist)
name = char(namelist(it));
if ~isfield(this.timeSeries, name)
this.timeSeries.(name) = timeseries;
end
this.timeSeries.(name) = addsample(this.timeSeries.(name), 'Data', this.structToSave.(name), 'Time', time);
end
end
It works pretty good. At the end of the simulation I save the "timeSeries" struct to a workspace variable and that's it.
But this attemp is way to slow. If I have a list of ~30-40 signals to save you can heavily feel the simulation getting slower.
Does someone has any better alternative to that? Or is it only my programing that sucks here? ;)
Thanks a lot!
2 Kommentare
Nitin Kapgate
am 16 Dez. 2020
Is all the data in the structure of numeric type?
Simon Meyer
am 16 Dez. 2020
Bearbeitet: Simon Meyer
am 17 Dez. 2020
Akzeptierte Antwort
Weitere Antworten (1)
Nitin Kapgate
am 16 Dez. 2020
0 Stimmen
As the data is numeric/boolean, I would suggest that you to write the data at every step or periodically to a file (say a CSV file with columns for different signals and rows for every step). That would help in increasing the RAM available for simulation and improve the simulation speed. At the simulation progresses, the growing size of logged data can consume huge amount of RAM and slow down simulations.
4 Kommentare
Simon Meyer
am 17 Dez. 2020
Simon Meyer
am 17 Dez. 2020
Bearbeitet: Simon Meyer
am 17 Dez. 2020
Simon Meyer
am 17 Dez. 2020
Nitin Kapgate
am 18 Dez. 2020
I am glad that the issue is resolved.
Kategorien
Mehr zu Structures finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!