Storing values from function in loop
이전 댓글 표시
Basic question but its driving me crazy...if I have the following loop iterated through a function how would i store the values in their own matrix instead of having the stream of numbers just pop up?
x = {A(:,3) B(:,3) C(:,3) D(:,3) E(:,3)};
for i = 1: length(x)
DaySum(x{i});
end;
Totalrun =
614
Totalrun =
516
Totalrun =
633
Totalrun =
597
Totalrun =
630
댓글 수: 1
Adam
2017년 8월 25일
Just assign the result to an indexed matrix e.g.
res(i) = DaySum(x{i});
답변 (1개)
Star Strider
2017년 8월 25일
I don’t see where your ‘TotalRun’ variable is defined in the code you posted.
I would index it in the array:
TotalRun(i) = ...;
댓글 수: 3
Joe
2017년 8월 25일
편집: Walter Roberson
2017년 8월 25일
Stephen23
2017년 8월 25일
1) In order to suppress displaying any line of code it must have a semi-colon at the end. Compare these two lines:
TotalSum=sum(t) % displays in the command window
TotalSum=sum(t); % not displayed in the command window
^ here I am!
2) The output is not defined, thus the error message that you showed us. You define the function with this line, which defines one input t and one output x:
function x=DaySum(t)
but nowhere in the code inside the function is x ever defined. How do you expect to return a value x if you never tell MATLAB what x is? Possibly you meant to write this:
function x=DaySum(t)
x=sum(t);
end
I also removed the semi-colon after the end, which did absolutely nothing.
Bonus tip: instead of writing code by guessing how MATLAB works, you should really do the introductory tutorials, which are highly recommended for all beginners:
and also read this:
Joe
2017년 8월 25일
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!