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

Just assign the result to an indexed matrix e.g.
res(i) = DaySum(x{i});

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2017년 8월 25일

0 개 추천

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
Joe 2017년 8월 25일
편집: Walter Roberson 2017년 8월 25일
I have the TotalRun defined in the function script. Its basically this:
function x=DaySum(t)
TotalRun=sum(t)
end;
-------------------
I get the following error when I try to store values like youre suggesting( This is how ive been trying to do it but its not working for some reason ):
Output argument "x" (and maybe others) not assigned during call to "DaySum".
Error in W (line 4)
TotalRun(i)=DaySum(x{i});
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
Joe 2017년 8월 25일
Yea I meant to define the function like you have it im losing my mind right now...

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

Joe
2017년 8월 25일

편집:

2017년 8월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by