Function returning multiple vectors
Mostra commenti meno recenti
Hi,
I'd like to assemble three vectors in a function then return them to the caller. Something along these lines:
function [A, B, C] = myFct()
A(0) = 'Lorem'; A(1) = 'ipsum';
B(1) = -1 B(2) = 10
C(0) = 'Dolor' C(1) = 'amet'
The length of A, B and C is always identical.
Any suggestion would be greatly appreciated! Many thanks in advance!
Risposta accettata
Più risposte (4)
Sean de Wolski
il 29 Giu 2011
[a b c] = func_name()
%define a b c
a = {'lorem','ipsum'}
b = [1 10]
c = {'Dolor','amet'}
end
Call it from the command line or another function with
[a b c] = func_name
You may also want to read about cell arrays since that's what you'll need for multiple strings.
doc cell
MATLAB begins indexing at one, not zero, by the way.
Macko
il 29 Giu 2011
0 voti
3 Commenti
Sean de Wolski
il 29 Giu 2011
if (u== 0)
A(1) = 'Lorem';
else
A(1) = 'Quisce';
end
Remember what I said about zero based indexing! (it doesn't exist in ML)
Paulo Silva
il 29 Giu 2011
you got the code almost done but like Sean said 'MATLAB begins indexing at one, not zero, by the way.'
if (u== 0)
A(1) = 'Lorem';
else
A(1) = 'Quisce';
end
Paulo Silva
il 29 Giu 2011
Sean you are too fast lol
Macko
il 29 Giu 2011
0 voti
4 Commenti
Sean de Wolski
il 29 Giu 2011
objectDs is an oputput not an input. to have it taken as an input
function [objectIDs, animationNames, animationValues] = TEST_vSendMultipleEvents(objectIDs);
doc function
is probably worth a read.
Macko
il 29 Giu 2011
Paulo Silva
il 29 Giu 2011
Embedded MATLAB Functions are always that annoying, the problem is that on those functions before you work with variables you must give them something of the same class (and probably size).
Initialize every variable you use with a value
objectIDs=[0 0];
animationNames=['' ''];
animationValues=[0 0];
%now insert your code
Macko
il 29 Giu 2011
Truong Dang Manh
il 8 Dic 2015
Modificato: Truong Dang Manh
il 8 Dic 2015
0 voti
Sorry to interrupt you guys but the code above does not work, you have to modify: "A(1) = 'Lorem';" to "A(1,:)='Lorem'; ", because what you're trying to do is to create an array of strings. See here for more information: http://stackoverflow.com/questions/7100841/create-an-array-of-strings. P/S: I'm new here so please correct me if i'm wrong
Categorie
Scopri di più su Cell Arrays in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!