Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to assign function outputs to matrix elements?

Subject: How to assign function outputs to matrix elements?

From: Sebastian

Date: 20 Jun, 2013 21:01:33

Message: 1 of 5

Hello everyone,

I've been trying to elegantly assign each of a function's multiple outputs to specific elements of an array, with no success.

Example:

% Function:
[one two three] = myfun()
one = 1;
two = 2;
three = 3;
end

% Script
answers = zeros(3,1)
[answers(2,1) , answers(3,1) , answers(1,1)] = myfun();

This kind of script does not work, and I'm currently using auxiliary variables to make redundant assignments:
[a,b,c] = myfun();
answers(2,1) = a;
answers(3,1) = b:
answers(1,1) = c;


How can I fix this problem???

Thank you

Subject: How to assign function outputs to matrix elements?

From: Nasser M. Abbasi

Date: 20 Jun, 2013 21:13:37

Message: 2 of 5

On 6/20/2013 4:01 PM, Sebastian wrote:
> Hello everyone,
>
> I've been trying to elegantly assign each of a function's multiple
>outputs to specific elements of an array, with no success.
>
> Example:
>
> % Function:
> [one two three] = myfun()
> one = 1;
> two = 2;
> three = 3;
> end
>
> % Script
> answers = zeros(3,1)
> [answers(2,1) , answers(3,1) , answers(1,1)] = myfun();
>
> This kind of script does not work, and I'm currently using auxiliary
> variables to make redundant assignments:
> [a,b,c] = myfun();
> answers(2,1) = a;
> answers(3,1) = b:
> answers(1,1) = c;
>
>
> How can I fix this problem???
>
> Thank you
>


Replace the line

        [one two three] = myfun()

with

       function [one two three] = myfun()

You need the word 'function' in there.

-------------------------
function foo( )

answers = zeros(3,1);
[answers(2) , answers(3) , answers(1)] = myfun();

answers
end

function [one two three] = myfun()
one = 1;
two = 2;
three = 3;
end
-----------------------------

EDU>> foo

answers =

      3
      1
      2



--Nasser

Subject: How to assign function outputs to matrix elements?

From: Sebastian

Date: 20 Jun, 2013 21:38:08

Message: 3 of 5

Nasser, that was just an example... Of course in the original files (which are far more complex) the functions work properly... The problem is in the assignment

"Nasser M. Abbasi" wrote in message <kpvr9u$ci6$1@speranza.aioe.org>...
> On 6/20/2013 4:01 PM, Sebastian wrote:
> > Hello everyone,
> >
> > I've been trying to elegantly assign each of a function's multiple
> >outputs to specific elements of an array, with no success.
> >
> > Example:
> >
> > % Function:
> > [one two three] = myfun()
> > one = 1;
> > two = 2;
> > three = 3;
> > end
> >
> > % Script
> > answers = zeros(3,1)
> > [answers(2,1) , answers(3,1) , answers(1,1)] = myfun();
> >
> > This kind of script does not work, and I'm currently using auxiliary
> > variables to make redundant assignments:
> > [a,b,c] = myfun();
> > answers(2,1) = a;
> > answers(3,1) = b:
> > answers(1,1) = c;
> >
> >
> > How can I fix this problem???
> >
> > Thank you
> >
>
>
> Replace the line
>
> [one two three] = myfun()
>
> with
>
> function [one two three] = myfun()
>
> You need the word 'function' in there.
>
> -------------------------
> function foo( )
>
> answers = zeros(3,1);
> [answers(2) , answers(3) , answers(1)] = myfun();
>
> answers
> end
>
> function [one two three] = myfun()
> one = 1;
> two = 2;
> three = 3;
> end
> -----------------------------
>
> EDU>> foo
>
> answers =
>
> 3
> 1
> 2
>
>
>
> --Nasser

Subject: How to assign function outputs to matrix elements?

From: Nasser M. Abbasi

Date: 20 Jun, 2013 22:21:56

Message: 4 of 5

On 6/20/2013 4:38 PM, Sebastian wrote:
> Nasser, that was just an example... Of course in the original files
>(which are far more complex) the functions work properly... The problem is in the assignment

what is the problem with the assignment? It works correctly. You did not say
what the problem is. What is the problem with this?

>> -------------------------
>> function foo( )
>>
>> answers = zeros(3,1);
>> [answers(2) , answers(3) , answers(1)] = myfun();
>>
>> answers
>> end
>>
>> function [one two three] = myfun()
>> one = 1;
>> two = 2;
>> three = 3;
>> end
>> -----------------------------
>>
>> EDU>> foo
>>
>>
>> 3
>> 1
>> 2


In the above, answers(2) has value 1, which maps to 'one' in the function
In the above, answers(3) has value 2, which maps to 'two' in the function
In the above, answers(1) has value 3, which maps 'three' in the function

Again, where is the problem?

Which is displayed at the end, is the vector 'answers'

--Nasser

Subject: How to assign function outputs to matrix elements?

From: Steven_Lord

Date: 21 Jun, 2013 15:35:53

Message: 5 of 5



"Sebastian " <seba.rivas@gmail.com> wrote in message
news:kpvqjd$qn4$1@newscl01ah.mathworks.com...
> Hello everyone,
>
> I've been trying to elegantly assign each of a function's multiple outputs
> to specific elements of an array, with no success.
> Example:
>
> % Function:
> [one two three] = myfun()
> one = 1;
> two = 2;
> three = 3;
> end
>
> % Script
> answers = zeros(3,1)
> [answers(2,1) , answers(3,1) , answers(1,1)] = myfun();
>
> This kind of script does not work, and I'm currently using auxiliary
> variables to make redundant assignments:

That worked just fine for me. Perhaps you're getting confused by the fact
that the displayed answers contains all zero values. Note that you're
displaying (by forgetting the semicolon at the end) the answers vector
_created by ZEROS_ not the answers vector after it was modified by the
assignment of myfun's output arguments. If you modified your code to:

answers = zeros(3,1);
[answers(2,1) , answers(3,1) , answers(1,1)] = myfun();
answers

it should display answers as [3; 1; 2]. If that's not what you're seeing you
will need to elaborate on what "does not work" means -- does it throw an
error? Does it give you different answers than you expect?

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us