Filter löschen
Filter löschen

Save content in a for loop

4 Ansichten (letzte 30 Tage)
Mouse
Mouse am 2 Nov. 2021
Kommentiert: Walter Roberson am 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

Antworten (1)

Walter Roberson
Walter Roberson am 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 Kommentare
Mouse
Mouse am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by