필터 지우기
필터 지우기

Save content in a for loop

조회 수: 4 (최근 30일)
Mouse
Mouse 2021년 11월 2일
댓글: Walter Roberson 2021년 11월 8일
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에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by