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:
multiple assignment of vector elements to variables matlab

Subject: multiple assignment of vector elements to variables matlab

From: Luca Cerone

Date: 5 Jul, 2010 15:50:08

Message: 1 of 7

Hi everybody,
I'm sorry if the question is silly,
but I couldn't find in the help nor in the newsgroup.

I've a vector v and have to assign its values to different variables,
something like:

a=v(1);
b=v(2);
c=v(3);
.
.
.
and so on.

Is there any easy way to write such a piece of code?
Thanks a lot in advance to you all.
Cheers, -Luca

Subject: multiple assignment of vector elements to variables matlab

From: Matt Fig

Date: 5 Jul, 2010 16:38:03

Message: 2 of 7

Would you mind explaining why you would want to do such a thing? Usually the reason for doing something like this won't justify the further trouble you will get later on in the code.

Subject: multiple assignment of vector elements to variables matlab

From: Faraz Afzal

Date: 5 Jul, 2010 16:39:15

Message: 3 of 7

"Luca Cerone" <luca_cerone#_remove_this#@yahoo.it> wrote in message <i0suvg$eou$1@fred.mathworks.com>...
> Hi everybody,
> I'm sorry if the question is silly,
> but I couldn't find in the help nor in the newsgroup.
>
> I've a vector v and have to assign its values to different variables,
> something like:
>
> a=v(1);
> b=v(2);
> c=v(3);
> .
> .
> .
> and so on.
>
> Is there any easy way to write such a piece of code?
> Thanks a lot in advance to you all.
> Cheers, -Luca

Hi Luca,

I wonder your use of this thing..

Hint..

Go for Doc char
Then look for ASCII characters from 97.,...122
May it helps
Regards
Faraz

Subject: multiple assignment of vector elements to variables matlab

From: us

Date: 5 Jul, 2010 16:52:07

Message: 4 of 7

"Luca Cerone" <luca_cerone#_remove_this#@yahoo.it> wrote in message <i0suvg$eou$1@fred.mathworks.com>...
> Hi everybody,
> I'm sorry if the question is silly,
> but I couldn't find in the help nor in the newsgroup.
>
> I've a vector v and have to assign its values to different variables,
> something like:
>
> a=v(1);
> b=v(2);
> c=v(3);
> .
> .
> .
> and so on.
>
> Is there any easy way to write such a piece of code?
> Thanks a lot in advance to you all.
> Cheers, -Luca

well... since you're a seasoned ML/CSSM user, let's assume you have very good reason for doing this...

one of the solutions

     v=1:3;
     v=num2cell(v);
     [a,b,c]=deal(v{:})
% a = 1
% b = 2
% c = 3

us

Subject: multiple assignment of vector elements to variables matlab

From: Jan Simon

Date: 5 Jul, 2010 20:20:06

Message: 5 of 7

Dear Luca,

> us wrote:
> well... since you're a seasoned ML/CSSM user, let's assume you have very good reason for doing this...
> v=1:3;
> v=num2cell(v);
> [a,b,c]=deal(v{:})

But for less experienced Matlab users: Prefer other solutions, e.g.:
- Stay at the vector, vectors are nice.
- Dynamic field names:
   for k = 1:numel(v)
     data.(char(96 + k)) = v(k);
   end

Jan

Subject: multiple assignment of vector elements to variables matlab

From: Luca Cerone

Date: 5 Jul, 2010 21:47:05

Message: 6 of 7

Thanks guys,
but actually I wanted to know if there is a built-in function..
similar to the deal for cells!
As we're talking, why wouldn't you suggest to assign variables
such a way?
I have a vector vec of parameters (I get it from fmincon).
I have to use these parameters into an ode,
so instead of remembering the order of the parameters
in the vector I prefer to create a function [a1,a2,a3]=setParam(param)
that assign the right value to each parameter.
This way if I make a mistake I only have to change the setParam function
instead of looking in the code everytime I'm using it.
If there is a more effective/elegant way any advice is accepted!
Cheers, -Luca

Subject: multiple assignment of vector elements to variables matlab

From: Walter Roberson

Date: 5 Jul, 2010 21:53:40

Message: 7 of 7

Luca Cerone wrote:
> Thanks guys,
> but actually I wanted to know if there is a built-in function..
> similar to the deal for cells!

Sorry, No there is not. Converting to cell and using deal on the {:} of the
cell is the only mechanism for mass assignment.

> As we're talking, why wouldn't you suggest to assign variables
> such a way?

http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F


 > I have to use these parameters into an ode,
 > so instead of remembering the order of the parameters
 > in the vector I prefer to create a function [a1,a2,a3]=setParam(param)
 > that assign the right value to each parameter.
 > This way if I make a mistake I only have to change the setParam function
 > instead of looking in the code everytime I'm using it.
 > If there is a more effective/elegant way any advice is accepted!

I suggest you consider using fields of a structure. You may wish to use
cell2struct to build the structure.

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