How can I store the value of variable 'dist_5thwheel_to_second_axle_sem2_itr' in array/vector format with every for loop iteration such that previous value stays as well? ?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
MAYANK DHYANI
am 14 Aug. 2021
Kommentiert: Scott MacKenzie
am 14 Aug. 2021
dist_5thwheel_to_second_axle_sem2_itr = [7.13, 7.63];
dist_front_trailer_to_5thwheel_sem2_itr = [1.368, 1.668]; %variable whose value needs to be stored with each iteration
counter1 = length(dist_5thwheel_to_second_axle_sem2_itr);
counter2 = length(dist_front_trailer_to_5thwheel_sem2_itr);
a1 = zeros(1,4); %total number of simulation runs are 4, so preallocating with size 4 row-vector
for i1 = 1:counter1 %counter for parameter1
dist_5thwheel_to_second_axle_sem2 = dist_5thwheel_to_second_axle_sem2_itr(i1);
for i2 = 1:counter1 %counter for parameter2
dist_front_trailer_to_5thwheel_sem2 = dist_front_trailer_to_5thwheel_sem2_itr(i2);
a1 = dist_5thwheel_to_second_axle_sem2_itr(i1) ; %a1 should store all the values previous and updated
%assigniing the simulation parameters to base workspace
assignin('base','tmax',tmax);
assignin('base','dt',dt);
assignin('base','Vx',Vx);
assignin('base','X0',X0);
assignin('base','rdfname',rdfname);
assignin('base','rdfname_inner',rdfname_inner);
assignin('base','steer_input',steer_input);
assignin('base','brake_input',brake_input);
assignin('base','throttle_input',throttle_input);
assignin('base','Vx_input',Vx_input);
assignin('base','end_posy',end_posy)
assignin('base','steer_sens_c',steer_sens_c);
assignin('base','steer_ratio',steer_ratio);
assignin('base','steer_control',steer_control);
assignin('base','path_input',path_input);
assignin('base','lookahead_time',lookahead_time);
assignin('base','T_lag',T_lag);
modelname = 'SEC_final';
sim(modelname,[],simset('DstWorkspace','base'));
evalin('base',['save ..\simresults\',modelname,' s VR dt'])
end
end
0 Kommentare
Akzeptierte Antwort
Scott MacKenzie
am 14 Aug. 2021
Bearbeitet: Scott MacKenzie
am 14 Aug. 2021
You've got a bug in the setup of your loops. For the inner loop, change
for i2 = 1:counter1
to
for i2 = 1:counter2
As for maintaining the values of a1 from iteration to iteration, here's a simple way to do this that avoids pre-allocation. In the line before the outer loop begins, change
a1 = zeros(1,4);
to
a1 = []; % create a1, empty at first
Then, change the assignment to a1 in the inner loop to
a1 = [a1 dist_5thwheel_to_second_axle_sem2_itr(i1) ;
Do this, and you'll achieve the objective stated in your question.
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!