Function returning multiple vectors

조회 수: 38 (최근 30일)
Macko
Macko 2011년 6월 29일
편집: Truong Dang Manh 2015년 12월 8일
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!

채택된 답변

Paulo Silva
Paulo Silva 2011년 6월 29일
function [A B C]=myFct
%if the values have diferent sizes or classes replace [] with {}
A=['Lorem' 'ipsum'];
B=[-1 10];
C=['Dolor' 'amet'];
end

추가 답변 (4개)

Sean de Wolski
Sean de Wolski 2011년 6월 29일
[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
Macko 2011년 6월 29일
That was incredibly fast! Many-many thanks!
Now, I can see I have simplified my problem too much. I actually want to set up the array value after doing some calculations first. The code would more likely be something like this:
function [A, B, C] = myFct(u)
if (u== 0) A(0) = 'Lorem'; else A(0) = 'Quisce'; endif
if (u<= 100) A(1) = 'ipsum'; else A(1) = 'dorum'; endif
Similar for the other two parameters.
Thanks a again!
  댓글 수: 3
Paulo Silva
Paulo Silva 2011년 6월 29일
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
Paulo Silva 2011년 6월 29일
Sean you are too fast lol

댓글을 달려면 로그인하십시오.


Macko
Macko 2011년 6월 29일
You guys are the best! :)
Here's my code:
function [objectIDs, animationNames, animationValues] = TEST_vSendMultipleEvents % This block supports the Embedded MATLAB subset. % See the help menu for details.
objectIDs(1) = -1;
animationNames(1)='Anim_1';
animationValues(1) = 1;
objectIDs(2) = -1;
animationNames(2)='Anim_2';
animationValues(2) = 100;
I'm getting this error: Undefined function or variable 'objectIDs'. The first assignment to a local variable determines its class.
Function 'Embedded MATLAB Function' (#40.166.175), line 5, column 1: "objectIDs"
Must be something really trivial, I'm overseeing here...
  댓글 수: 4
Paulo Silva
Paulo Silva 2011년 6월 29일
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
Macko 2011년 6월 29일
That seems to have solved the problem! Many thanks to both of you!

댓글을 달려면 로그인하십시오.


Truong Dang Manh
Truong Dang Manh 2015년 12월 8일
편집: Truong Dang Manh 2015년 12월 8일
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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by