Save content in a for loop

1 次查看(过去 30 天)
Mouse
Mouse 2021-11-2
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

回答(1 个)

Walter Roberson
Walter Roberson 2021-11-2
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 个评论
Mouse
Mouse 2021-11-8
thanks for your answer.
The content of number = {...} is changing because the user puts more ore less content in the array. How should I solve this?
Walter Roberson
Walter Roberson 2021-11-8
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Monte Carlo Analysis 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by