Thread Subject: getting a list

Subject: getting a list

From: Isabel

Date: 30 Nov, 2008 19:34:02

Message: 1 of 5

How can I get matlab to give me a list as the answer to a function? For example
function [a,b]=add(T)
a=2*T
b=3*T
[2*T,3*T]
everytime I do something like that, it only gives me back the first element. So in this case it would give me as an answer only 2*T.
What I've been doing is this:
function z=add(T)
z(1)=2*T
z(2)=3*T
end
but I would like to know how to put a list as an output argument. Thanks!

Subject: getting a list

From: Walter Roberson

Date: 30 Nov, 2008 19:52:36

Message: 2 of 5

Isabel wrote:
> How can I get matlab to give me a list as the answer to a function? For example
> function [a,b]=add(T)
> a=2*T
> b=3*T
> [2*T,3*T]
> everytime I do something like that, it only gives me back the first element. So in this case it would give me as an answer only 2*T.
> What I've been doing is this:
> function z=add(T)
> z(1)=2*T
> z(2)=3*T
> end
> but I would like to know how to put a list as an output argument. Thanks!

function z = add(T)
z = [2*T, 3*T]
end



--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: getting a list

From: Ryan Ollos

Date: 30 Nov, 2008 23:52:02

Message: 3 of 5

"Isabel" <isabels29@hotmail.com> wrote in message <ggupr9$280$1@fred.mathworks.com>...
> What I've been doing is this:
> function z=add(T)
> z(1)=2*T
> z(2)=3*T
> end
> but I would like to know how to put a list as an output argument. Thanks!

Your function seems to work fine for me. I get z = [10 15] as the return argument. Maybe you have a conflict with the built-in add function. Run 'which add -all' and check if your function is being shadowed ... or just rename it.

Subject: getting a list

From: Matt Fig

Date: 1 Dec, 2008 00:55:04

Message: 4 of 5

"Isabel" <isabels29@hotmail.com> wrote in message <ggupr9$280$1@fred.mathworks.com>...
> How can I get matlab to give me a list as the answer to a function? For example
> function [a,b]=add(T)
> a=2*T
> b=3*T
> everytime I do something like that, it only gives me back the first element. So in this case it would give me as an answer only 2*T.

I am going to guess you meant something different. Do you mean, "How do I call a function so that it will return several variables?" If so, say you have this function:

function [a,b]=add2(T) % Don't mask a built-in
a=2*T;
b=3*T;

Now at the command line call the function like this (for example):

>>[A,B]=add2(5)

You should have two new variables in your workspace. The variable 'A' has a value of 10 and the variable 'B' has a value of 15.

Subject: getting a list

From: swgillan

Date: 1 Dec, 2008 06:26:52

Message: 5 of 5

On Nov 30, 11:34=A0am, "Isabel" <isabel...@hotmail.com> wrote:
> How can I get matlab to give me a list as the answer to a function? For e=
xample
> function [a,b]=3Dadd(T)
> a=3D2*T
> b=3D3*T
> [2*T,3*T]
> everytime I do something like that, it only gives me back the first eleme=
nt. So in this case it would give me as an answer only 2*T.
> What I've been doing is this:
> function z=3Dadd(T)
> z(1)=3D2*T
> z(2)=3D3*T
> end
> but I would like to know how to put a list as an output argument. Thanks!

when you have a multi-variable output, you have to call it using an
array of the output you want. Otherwise, if you just call with 1
variable, you only get the first condition back:

z =3D add(T) will only return the first output of your function
[x y] =3D add(T) will return both properly.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com