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:
Array of variables

Subject: Array of variables

From: Shaun Hurley

Date: 20 Sep, 2011 15:59:30

Message: 1 of 8

Does anyone know how I can create an array of variables? I am looking for something like this:

U = [u1 u2 u3 u4, .... un]

Subject: Array of variables

From: Shaun Hurley

Date: 20 Sep, 2011 17:02:30

Message: 2 of 8

I should clarify:

I am looking to use a for loop for this, for example:

for i=1:n
U = u_i
end

... or something like that

Subject: Array of variables

From: Barry Williams

Date: 20 Sep, 2011 18:20:30

Message: 3 of 8

"Shaun Hurley" wrote in message <j5agv6$dku$1@newscl01ah.mathworks.com>...
> I should clarify:
>
> I am looking to use a for loop for this, for example:
>
> for i=1:n
> U = u_i
> end
>
> ... or something like that

This may still need clarification. Why won't
U(n) = yourFunction
work for you?

You would want to preallocate of course.

Barry

Subject: Array of variables

From: dpb

Date: 20 Sep, 2011 19:24:19

Message: 4 of 8

On 9/20/2011 10:59 AM, Shaun Hurley wrote:
> Does anyone know how I can create an array of variables? I am looking
> for something like this:
>
> U = [u1 u2 u3 u4, .... un]

Why (as yet unbeknownst to you :) ) you _do_NOT_ want to do that, and
suggested alternatives...

<http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F>

--

Subject: Array of variables

From: Steven_Lord

Date: 20 Sep, 2011 21:42:40

Message: 5 of 8



"Shaun Hurley" <jumpinghurley@yahoo.com> wrote in message
news:j5agv6$dku$1@newscl01ah.mathworks.com...
> I should clarify:
>
> I am looking to use a for loop for this, for example:
>
> for i=1:n
> U = u_i
> end
>
> ... or something like that

It's still unclear. Start at the beginning. Assume (and this will be a
correct assumption) that your audience has no knowledge of what you're
trying to do. Be specific about your goal.

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

Subject: Array of variables

From: Nasser M. Abbasi

Date: 21 Sep, 2011 22:04:51

Message: 6 of 8

On 9/20/2011 8:59 AM, Shaun Hurley wrote:
> Does anyone know how I can create an array of variables? I am looking for something like this:
>
> U = [u1 u2 u3 u4, .... un]

may be

EDU>> my_first_variable = 5;
EDU>> my_second_variable = 7;
EDU>> my_array_of_variables=[ my_first_variable my_second_variable]

my_array_of_variables =

      5 7

but it is better to use matrices and vector and store your 'variables'
as entries in them. Playing games with actual variable names is bad
programming.

--Nasser

Subject: Array of variables

From: Shaun Hurley

Date: 23 Sep, 2011 20:48:11

Message: 7 of 8

I am looking to generate an array of characters (not variables; sorry I didn't realize that these are not called variables in computer programming). I am just looking to have an array of characters generated for any given size. For example:
I want an array with three characters: Output should be u = [u1 u2 u3]
Or maybe I want an array of 7 characters: Output should be u = [u1 u2 u3 u4 u5 u6 u7]
I'm sure this must be possible, does anyone know?

Subject: Array of variables

From: Steven_Lord

Date: 23 Sep, 2011 21:29:45

Message: 8 of 8



"Shaun Hurley" <jumpinghurley@yahoo.com> wrote in message
news:j5irab$7nn$1@newscl01ah.mathworks.com...
> I am looking to generate an array of characters (not variables; sorry I
> didn't realize that these are not called variables in computer
> programming). I am just looking to have an array of characters generated
> for any given size. For example:
> I want an array with three characters: Output should be u = [u1 u2 u3]

That contains either six or eight characters, depending on whether or not
you want spaces:

u = 'u1u2u3';
% or
u = 'u1 u2 u3';

If you want u(1) [or something similar] to return 'u1' then you actually
want something different.

> Or maybe I want an array of 7 characters: Output should be u = [u1 u2 u3
> u4 u5 u6 u7]
> I'm sure this must be possible, does anyone know?

What do you want to DO with this array? That will determine how you will
want to construct it.

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

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