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:
call variable name

Subject: call variable name

From: Adam Chapman

Date: 10 Apr, 2008 19:17:41

Message: 1 of 4

Hi,

I have a load of variables with nemes a1,a2,a3 ... etc but i do not
know how many in total, the amount may be different every time i run
the code.

I want to be able to call them all to be used individually for further
processing. Does anybody know how to call a variable by its name?

Adam

Subject: call variable name

From: Adam Chapman

Date: 10 Apr, 2008 19:21:27

Message: 2 of 4

On Apr 10, 8:17=A0pm, Adam Chapman
<adam.chap...@student.manchester.ac.uk> wrote:
> Hi,
>
> I have a load of variables with nemes a1,a2,a3 ... etc but i do not
> know how many in total, the amount may be different every time i run
> the code.
>
> I want to be able to call them all to be used individually for further
> processing. Does anybody know how to call a variable by its name?
>
> Adam

Dont think I explained very well.
Imagine my code can tell how many variables of a_ exist, and i want to
access a_n to use in an equation.

Subject: call variable name

From: someone

Date: 10 Apr, 2008 19:35:03

Message: 3 of 4

Adam Chapman <adam.chapman@student.manchester.ac.uk> wrote
in message <6742c369-b684-42cf-b48e-
efae76b0bbfe@w4g2000prd.googlegroups.com>...
> On Apr 10, 8:17=A0pm, Adam Chapman
> <adam.chap...@student.manchester.ac.uk> wrote:
> > Hi,
> >
> > I have a load of variables with nemes a1,a2,a3 ... etc
but i do not
> > know how many in total, the amount may be different
every time i run
> > the code.
> >
> > I want to be able to call them all to be used
individually for further
> > processing. Does anybody know how to call a variable by
its name?
> >
> > Adam
>
> Dont think I explained very well.
> Imagine my code can tell how many variables of a_ exist,
and i want to
> access a_n to use in an equation.
>

The short answer is don't do this, use cell arrays instead.

The long answer: this is very messy, requires you to use
EVAL throughout
your code, and can be very difficult to debug, so you
shouldn't do this. If
you must, see Q4.6 in the newsgroup FAQ at:

http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: call variable name

From: Yuri Geshelin

Date: 10 Apr, 2008 19:40:18

Message: 4 of 4

Adam Chapman <adam.chapman@student.manchester.ac.uk> wrote
in message <6742c369-b684-42cf-b48e-
efae76b0bbfe@w4g2000prd.googlegroups.com>...
> On Apr 10, 8:17=A0pm, Adam Chapman
> <adam.chap...@student.manchester.ac.uk> wrote:
> > Hi,
> >
> > I have a load of variables with nemes a1,a2,a3 ... etc
but i do not
> > know how many in total, the amount may be different
every time i run
> > the code.
> >
> > I want to be able to call them all to be used
individually for further
> > processing. Does anybody know how to call a variable by
its name?
> >
> > Adam
>

Hi,

People ask this question frequently.

Suppose all you have is a1, a2, a3.

You can do

>> w = whos('a*');

Then w(2).name will return a2.

But matlab specialists discourage people from doing these
things. Better use a(1), a(2) or a{1}, a{2}.

Yuri

> Dont think I explained very well.
> Imagine my code can tell how many variables of a_ exist,
and i want to
> access a_n to use in an equation.
>

Tags for 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