Can i debug a for loop ?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Daniel Wurster
am 24 Jun. 2019
Kommentiert: infinity
am 25 Jun. 2019
Hello, i clicked through the answers, but it didnt help.
What am i trying to do? Put for each round of the loop two values in file. Overall 9 rounds, two values each -> 18 values
What have i done? - I've created two 9x6 matrices called xwerte and ywerte
- I've created two arrays called xd_ref and yd_ref
Script looks like this:
% Loop goes trough each row
for j=1:length(xwerte)
% loope goes through each column of specific row
for i=1:length(xwerte(j,:))
% Simple function that gives out two values and puts it into file
[S, ab] = dist_pt2refTraj(xwerte(j,i), ywerte(j,i), xd_ref, yd_ref);
save('trajektorien.mat', 'S', 'ab');
if ab > 20
error('Error. \n ab is to big')
end
end
end
If i click on run, i get in my file only two values, not 18.
How can i debug through each loop? Or whats the problem here?
Thanks a lot.
0 Kommentare
Akzeptierte Antwort
infinity
am 24 Jun. 2019
Hello,
In your code, you save variables "S" and "ab" with the same name for each loop. Therefore, the 'trajektorien.mat' only stores the value of the last loop.
To avoid this problem you should save "S" and "ab" for each loop to a vector. Then just save the vector. Because of depending on how do you want to store data, below could be a possible solution
saveS_ab = [];
% Loop goes trough each row
for j=1:length(xwerte)
% loope goes through each column of specific row
for i=1:length(xwerte(j,:))
% Simple function that gives out two values and puts it into file
[S, ab] = dist_pt2refTraj(xwerte(j,i), ywerte(j,i), xd_ref, yd_ref);
saveS_ab = [saveS_ab; S ab];
save('trajektorien.mat', 'saveS_ab');
if ab > 20
error('Error. \n ab is to big')
end
end
end
Now, you data is store in varibale "saveS_ab"
Check does it work or not.
4 Kommentare
infinity
am 25 Jun. 2019
Thank @Jan.
These sugguestions will optimize the code and save time for the case of big data.
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!