How to save function outputs as a vector to workspace.

28 Ansichten (letzte 30 Tage)
Richard
Richard am 30 Jul. 2014
Kommentiert: Ikenna Okoye am 31 Aug. 2018
function [A B] = coconuts( a, b, c)
the function will run many times, each time outputting a unique A & B. How can I save each A & B output into a corresponding (N,1) vector that is available in the workspace?
Thank you.

Akzeptierte Antwort

Ahmet Cecen
Ahmet Cecen am 30 Jul. 2014
Your question is worded in an ambiguous way, I am not sure I understand your exact question. I will speculate a bit and hope one of them answer your question.
1) Your A and B are scalars:
ResultsA=zeros(N,1);
ResultsB=zeros(N,1);
for i=1:N
[A B] = coconuts( a, b, c);
ResultsA(i)= A;
ResultsB(i)= B;
end
2) Your A and B are vectors:
ResultsA=zeros(length(A),N); %Lenght A has to be input manually since you havent computed A.
ResultsB=zeros(length(B),N); %Same
for i=1:N
[A B] = coconuts( a, b, c);
ResultsA(:,i)= A;
ResultsB(:,i)= B;
end
3) Your A and B are matrices:
ResultsA=cell(N,1);
ResultsB=cell(N,1);
for i=1:N
[A B] = coconuts( a, b, c);
ResultsA{i}= A;
ResultsB{i}= B;
end
Give more details if none of these helps.

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 30 Jul. 2014
for k=1:N
[A(k) B(k)] = coconuts( a, b, c);
end
  1 Kommentar
Ikenna Okoye
Ikenna Okoye am 31 Aug. 2018
It works nicely, but is there a way to increment the k=1:N? I wrote a code for an atmospheric model and I need to plot the output of the function and I'm varying it from 1km to 100km by 1km increments. I would appreciate your help!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Objects finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by