how can i save variables in a for loop?

23 visualizzazioni (ultimi 30 giorni)
chirag vatiya
chirag vatiya il 31 Gen 2020
Commentato: ME il 3 Feb 2020
I am not familiar with the coding in MATLAB. This is the code for passive suspension system. I am trying to get different variables to save in .mat file in every run of simulation.
For example,
with n=1, the data runs for design 1 and all the variables save into struct named "passive" and these variables Pitchrate1, Rollrate1, acc_z1, F_L1, p_L1, velo_z1 and Q_L1 should be saved in a new file named DesignResults_1.mat.
with n=2, the data runs for design 2 and all the variables save into struct named "passive" (the same struct passive as of first run) and these variables Pitchrate2, Rollrate2, acc_z2, F_L2, p_L2, velo_z2 and Q_L2 should be saved in a new file named DesignResults_2.mat. and so on...
At the end of simulation i want to get 4 files named DesignResults_1.mat, DesignResults_2.mat, DesignResults_3.mat and DesignResults_4.mat.
Any types of help would be appreciated. Thank you in advance.
%% Active Suspension Simulation
gain = 0;
for n = 1:1:4
model = strcat('JD6190R_DesignExample_',num2str(n),'.slx');
open_system(model);
set_param(strcat('JD6190R_DesignExample_',num2str(n),'/Active suspension System/DampingConstant'),'Gain',num2str(gain))% set the paramter to 0
set_param(strcat('JD6190R_DesignExample_',num2str(n),'/Active suspension System/DampingConstant1'),'Gain',num2str(gain))% set the paramter to 0
simOut = sim(model,'StopTime','10','ReturnWorkspaceOutputs','on', 'SaveOutput','on');
eval(['passive.Pitchrate' num2str(n) '= simOut.Pitchrate' num2str(n) ';']);
eval(['passive.Rollrate' num2str(n) '= simOut.Rollrate' num2str(n) ';']);
eval(['passive.acc_z' num2str(n) '= simOut.acc_z' num2str(n) ';']);
eval(['passive.F_L' num2str(n) '= simOut.F_L' num2str(n) ';']);
eval(['passive.p_L' num2str(n) '= simOut.p_L' num2str(n) ';']);
eval(['passive.velo_L' num2str(n) '= simOut.velo_L' num2str(n) ';']);
eval(['passive.Q_L' num2str(n) '= simOut.Q_L' num2str(n) ';']);
save_system(model);
close_system(model);
  1 Commento
Stephen23
Stephen23 il 31 Gen 2020
Modificato: Stephen23 il 31 Gen 2020
"I am quiet familiar with the coding in MATLAB."
Then you should be quite familier with all of the reasons why your code approach is not recommended:
Stop forcing yourself into writing slow, complex, obfuscated, buggy code that is hard to debug, simply by avoiding eval.
None of your eval calls are required, you can rewrite your code quite simply using dynamic fieldnames:
Although i strongly recommend that to make your code simpler and more effiicient you should actually use exactly the same variable names in each file. Then looping over your files would be trivial.
Anyone who is "quiet familiar with the coding in MATLAB" will also know that sprintf is probably a much better choice than num2str and string concatenation.

Accedi per commentare.

Risposte (1)

ME
ME il 31 Gen 2020
If your issue is how to get the file names then you can use:
save( strcat( 'DesignResults_', num2str(n) );
within your loop and that will save the outputs to the relevant file names.
  1 Commento
ME
ME il 3 Feb 2020
Of course, as Stephen Cobeldick has suggested above, you can also use sprintf to get the same effect as I have suggested above.

Accedi per commentare.

Categorie

Scopri di più su Variables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by