Clear Filters
Clear Filters

Save content in a for loop

4 views (last 30 days)
Mouse
Mouse on 2 Nov 2021
Commented: Walter Roberson on 8 Nov 2021
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
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
Mouse
Mouse on 8 Nov 2021
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 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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!