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!