Clear Filters
Clear Filters

Save all values of a calculation through a loop in a file

3 views (last 30 days)
V=[31:1:61];
angle=2;
for V=(31:1:61)
save myresults UVW0 -mat
end
Hello,
I need your help please!
I want to save all the results for UVW0 in a file bu my current program only saves the last results of the calculation in the mat file.
How can I save all the results for each calculation in the mat file?
Many thanks in advance!

Accepted Answer

Stephen23
Stephen23 on 22 Feb 2023
Edited: Stephen23 on 22 Feb 2023
"How can I save all the results for each calculation in the mat file?"
The standard, efficient, simple approach is to store the data in one array and save it once after the loop.
But why do you need a loop anyway? That calculation is easy to perform without a loop:
V = 31:1:61; % got rid of the superfluous square brackets too
angle = 2;
UVW0 = V .* [cos(angle);0;sin(angle)]
UVW0 = 3×31
-12.9006 -13.3167 -13.7328 -14.1490 -14.5651 -14.9813 -15.3974 -15.8136 -16.2297 -16.6459 -17.0620 -17.4782 -17.8943 -18.3105 -18.7266 -19.1428 -19.5589 -19.9750 -20.3912 -20.8073 -21.2235 -21.6396 -22.0558 -22.4719 -22.8881 -23.3042 -23.7204 -24.1365 -24.5527 -24.9688 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 28.1882 29.0975 30.0068 30.9161 31.8254 32.7347 33.6440 34.5533 35.4626 36.3719 37.2812 38.1905 39.0998 40.0091 40.9184 41.8277 42.7370 43.6463 44.5556 45.4649 46.3742 47.2835 48.1928 49.1021 50.0114 50.9207 51.8300 52.7393 53.6485 54.5578
save myresults.mat UVW0
  1 Comment
Joe
Joe on 22 Feb 2023
Thanks a lot!
I used the loop to calculate V*cos(angle) for each element of V from 31 to 61.

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!