Storing values from function in loop
Afficher commentaires plus anciens
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 commentaire
Adam
le 25 Août 2017
Just assign the result to an indexed matrix e.g.
res(i) = DaySum(x{i});
Réponses (1)
Star Strider
le 25 Août 2017
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 commentaires
Joe
le 25 Août 2017
Modifié(e) : Walter Roberson
le 25 Août 2017
Stephen23
le 25 Août 2017
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
le 25 Août 2017
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!