Save content in a for loop
1 view (last 30 days)
Show older comments
I have a for loop. In this loop I have a function with 3 returning arguments
Now I should save the content of this returning variables. The content is a 10*100 uint16 matrix.
number = {'test1' ,'text2', 'text3'} %A array with content
for i = 1: length(number) %Do something for every array element in number
[val1, val2, val3] = dosthg(number{i}) %function returns a 10*100 uint16matrix
%how to save the return values?
pos1(i) = val1; pos2(i) = val2; pos3(i) = val3;
%try 2
pos1 = [{pos1} {val1}];
plot(time,mean(pos1(i)));
hold on;
end
At the end all for iterations shoud draw a new line in a pot.
Now my problem is, how can I add the new data to the Array/Cell?
Thanks for your answer
Answers (1)
Walter Roberson
on 2 Nov 2021
number = {'test1' ,'text2', 'text3'} %A array with content
num_num = length(number);
pos1 = cell(num_num, 1);
pos2 = cell(num_num, 1);
pos3 = cell(num_num, 1);
for i = 1: num_num %Do something for every array element in number
[val1, val2, val3] = dosthg(number{i}) %function returns a 10*100 uint16matrix
pos1{i} = val1;
pos2{i} = val2;
pos3{i} = val3;
plot(time,mean(val1));
hold on;
end
2 Comments
Walter Roberson
on 8 Nov 2021
The line of code I posted,
num_num = length(number);
asks how much content the user entered into the array. Then I use that amount to allocate the variables and to control the looping.
In other words, the code already takes care of that.
See Also
Categories
Find more on Monte Carlo Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!