Got Questions? Get Answers.
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:
assinging values to structures without a for loop

Subject: assinging values to structures without a for loop

From: Dimitar Dimitrov

Date: 20 Jul, 2009 11:02:03

Message: 1 of 5

Hello,

I have a structure of the form S(1).q, S(2).q ... If I have a vector A = [1;2;...] and I want to assign S(1).q = A(1), S(2).q = A(2) ... without using a for loop what should I do.

I know for example that I can output the fields "q" of the structure S in a vector like
V = [S.q], but I don't know how tho make the reverse operation and assign values to the "q's" using some vectorized way (for example something like "[S.q]' = A").

Thanks,
Dimitar

Subject: assinging values to structures without a for loop

From: us

Date: 20 Jul, 2009 11:09:02

Message: 2 of 5

"Dimitar Dimitrov" <mail_mitko@example.com> wrote in message <h41irb$h4e$1@fred.mathworks.com>...
> Hello,
>
> I have a structure of the form S(1).q, S(2).q ... If I have a vector A = [1;2;...] and I want to assign S(1).q = A(1), S(2).q = A(2) ... without using a for loop what should I do.
>
> I know for example that I can output the fields "q" of the structure S in a vector like
> V = [S.q], but I don't know how tho make the reverse operation and assign values to the "q's" using some vectorized way (for example something like "[S.q]' = A").
>
> Thanks,
> Dimitar

one of the solutions

     v=1:4;
     s=struct('f',num2cell(v));
     s.f
%{
ans =
     1
ans =
     2
ans =
     3
ans =
     4
%}

us

Subject: assinging values to structures without a for loop

From: Dimitar Dimitrov

Date: 20 Jul, 2009 12:15:19

Message: 3 of 5

Hi us,
thanks for the post.

What you proposed simply creates a structure. If the structure already exists it will overwrite it and one will lose all other "stuff" in the structure.
All I want to do is update S(:).q with the contents of the vector A. Of course I could have other fields which I don't want to manipulate.

Foe example (using your notation)

v = [1;2;3]
s=struct('f1',num2cell(v),'f2',num2cell(v));

v1 = [5;6;7]
I want to update only s.f1 with the contents of the vector v1

Cheers,
Dimitar


"us " <us@neurol.unizh.ch> wrote in message <h41j8e$jde$1@fred.mathworks.com>...
> "Dimitar Dimitrov" <mail_mitko@example.com> wrote in message <h41irb$h4e$1@fred.mathworks.com>...
> > Hello,
> >
> > I have a structure of the form S(1).q, S(2).q ... If I have a vector A = [1;2;...] and I want to assign S(1).q = A(1), S(2).q = A(2) ... without using a for loop what should I do.
> >
> > I know for example that I can output the fields "q" of the structure S in a vector like
> > V = [S.q], but I don't know how tho make the reverse operation and assign values to the "q's" using some vectorized way (for example something like "[S.q]' = A").
> >
> > Thanks,
> > Dimitar
>
> one of the solutions
>
> v=1:4;
> s=struct('f',num2cell(v));
> s.f
> %{
> ans =
> 1
> ans =
> 2
> ans =
> 3
> ans =
> 4
> %}
>
> us

Subject: assinging values to structures without a for loop

From: us

Date: 20 Jul, 2009 13:37:02

Message: 4 of 5

"Dimitar Dimitrov" <mail_mitko@example.com> wrote in message <h41n4n$ato$1@fred.mathworks.com>...
> Hi us,
> thanks for the post.
>
> What you proposed simply creates a structure. If the structure already exists it will overwrite it and one will lose all other "stuff" in the structure.
> All I want to do is update S(:).q with the contents of the vector A. Of course I could have other fields which I don't want to manipulate.

well, that is NOT what you told CSSM in your OP; you said ...assign...
please, be more specific and have not other people waste their time...

one of the solutions

     v=1:4;
     s=struct('f',num2cell(v)); % <- create a struct
     nv=num2cell([10,100,1000]);
     [s([1,3:4]).f]=deal(nv{:}); % <- update a struct
     s.f
%{
ans = 10
ans = 2
ans = 100
ans = 1000
%}

us

Subject: assinging values to structures without a for loop

From: Dimitar Dimitrov

Date: 20 Jul, 2009 13:52:01

Message: 5 of 5

So the trick is to use "deal"

Thanks a lot,
Dimitar

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