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 Commento

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

Accedi per commentare.

Risposte (1)

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 Commenti

Joe
Joe il 25 Ago 2017
Modificato: Walter Roberson il 25 Ago 2017
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 il 25 Ago 2017
Yea I meant to define the function like you have it im losing my mind right now...

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

Joe
il 25 Ago 2017

Modificato:

il 25 Ago 2017

Community Treasure Hunt

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

Start Hunting!

Translated by