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:
copy a field from a structure to a different structure

Subject: copy a field from a structure to a different structure

From: Naresh Pai

Date: 20 Aug, 2009 02:29:05

Message: 1 of 7

I would like to copy values in a specific field of a structure to another field in a different structure. For e.g.

% define structure a
a(1).b =1;
a(2).b =2;

how do I copy the values of field b to field d in structure c ?

One possible method is:

for i=1:2
   c(i).d = a(i).b
end

Is there a faster way ?

Subject: copy a field from a structure to a different structure

From: Jos

Date: 20 Aug, 2009 06:28:03

Message: 2 of 7

"Naresh Pai" <npai@uark.edu> wrote in message <h6icdg$31o$1@fred.mathworks.com>...
> I would like to copy values in a specific field of a structure to another field in a different structure. For e.g.
>
> % define structure a
> a(1).b =1;
> a(2).b =2;
>
> how do I copy the values of field b to field d in structure c ?
>
> One possible method is:
>
> for i=1:2
> c(i).d = a(i).b
> end
>
> Is there a faster way ?

Use DEAL:

[c(1:numel(a)).d] = deal(a.b)

hth
Jos

Subject: copy a field from a structure to a different structure

From: Matt

Date: 20 Aug, 2009 08:58:03

Message: 3 of 7

"Jos " <#10584@fileexchange.com> wrote in message <h6iqdi$hmf$1@fred.mathworks.com>...

> Use DEAL:
>
> [c(1:numel(a)).d] = deal(a.b)
>


Or, if you have a recent version of MATLAB, you could do simply

[c(1:numel(a)).d]=a.b;

Subject: copy a field from a structure to a different structure

From: Naresh Pai

Date: 20 Aug, 2009 13:09:04

Message: 4 of 7

"Matt " <xys@whatever.com> wrote in message <h6j36q$7p1$1@fred.mathworks.com>...
> "Jos " <#10584@fileexchange.com> wrote in message <h6iqdi$hmf$1@fred.mathworks.com>...
>
> > Use DEAL:
> >
> > [c(1:numel(a)).d] = deal(a.b)
> >
>
>
> Or, if you have a recent version of MATLAB, you could do simply
>
> [c(1:numel(a)).d]=a.b;

Thanks Jos and Matt. Both solutions work. I will be using Matt's version.

Subject: copy a field from a structure to a different structure

From: Naresh Pai

Date: 22 Aug, 2009 22:50:02

Message: 5 of 7

"Naresh Pai" <npai@uark.edu> wrote in message <h6jhtg$k8c$1@fred.mathworks.com>...
> "Matt " <xys@whatever.com> wrote in message <h6j36q$7p1$1@fred.mathworks.com>...
> > "Jos " <#10584@fileexchange.com> wrote in message <h6iqdi$hmf$1@fred.mathworks.com>...
> >
> > > Use DEAL:
> > >
> > > [c(1:numel(a)).d] = deal(a.b)
> > >
> >
> >
> > Or, if you have a recent version of MATLAB, you could do simply
> >
> > [c(1:numel(a)).d]=a.b;
>
> Thanks Jos and Matt. Both solutions work. I will be using Matt's version.


%In a similar case, I have a variable:

a(1) =1; a(2) = 2;

%I would like to copy this to a structure b as follows
b(1).c = a(1); b(2).c = a(2);

Does anybody know of a for-loop-free solution ?

Subject: copy a field from a structure to a different structure

From: Bruno Luong

Date: 22 Aug, 2009 23:12:01

Message: 6 of 7

"Naresh Pai" <npai@uark.edu> wrote in message <h6psmq$mnr$1@fred.mathworks.com>...
> "Naresh Pai" <npai@uark.edu> wrote in message <h6jhtg$k8c$1@fred.mathworks.com>...
> > "Matt " <xys@whatever.com> wrote in message <h6j36q$7p1$1@fred.mathworks.com>...
> > > "Jos " <#10584@fileexchange.com> wrote in message <h6iqdi$hmf$1@fred.mathworks.com>...
> > >
> > > > Use DEAL:
> > > >
> > > > [c(1:numel(a)).d] = deal(a.b)
> > > >
> > >
> > >
> > > Or, if you have a recent version of MATLAB, you could do simply
> > >
> > > [c(1:numel(a)).d]=a.b;
> >
> > Thanks Jos and Matt. Both solutions work. I will be using Matt's version.
>
>
> %In a similar case, I have a variable:
>
> a(1) =1; a(2) = 2;
>
> %I would like to copy this to a structure b as follows
> b(1).c = a(1); b(2).c = a(2);
>
> Does anybody know of a for-loop-free solution ?

a=(1:2);

c = num2cell(a);
[b(1:length(c)).c] = c{:} % or = deal(c{:})

% Bruno

Subject: copy a field from a structure to a different structure

From: Naresh Pai

Date: 22 Aug, 2009 23:23:04

Message: 7 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h6pu01$h8o$1@fred.mathworks.com>...
> "Naresh Pai" <npai@uark.edu> wrote in message <h6psmq$mnr$1@fred.mathworks.com>...
> > "Naresh Pai" <npai@uark.edu> wrote in message <h6jhtg$k8c$1@fred.mathworks.com>...
> > > "Matt " <xys@whatever.com> wrote in message <h6j36q$7p1$1@fred.mathworks.com>...
> > > > "Jos " <#10584@fileexchange.com> wrote in message <h6iqdi$hmf$1@fred.mathworks.com>...
> > > >
> > > > > Use DEAL:
> > > > >
> > > > > [c(1:numel(a)).d] = deal(a.b)
> > > > >
> > > >
> > > >
> > > > Or, if you have a recent version of MATLAB, you could do simply
> > > >
> > > > [c(1:numel(a)).d]=a.b;
> > >
> > > Thanks Jos and Matt. Both solutions work. I will be using Matt's version.
> >
> >
> > %In a similar case, I have a variable:
> >
> > a(1) =1; a(2) = 2;
> >
> > %I would like to copy this to a structure b as follows
> > b(1).c = a(1); b(2).c = a(2);
> >
> > Does anybody know of a for-loop-free solution ?
>
> a=(1:2);
>
> c = num2cell(a);
> [b(1:length(c)).c] = c{:} % or = deal(c{:})
>
> % Bruno

Thanks Bruno. Works just perfect!

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