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:
Working with structures

Subject: Working with structures

From: pmassicotte

Date: 13 Jun, 2012 15:03:58

Message: 1 of 3

Hi everyone.

I was wondering if it was possible to do certain operation on
structures. Let's say I have this:

S(1).x = 1:10;
S(1).y = 11:20;

S(2).x = 1:10;
S(2).y = 21:30;

Is it possible to create a field 'z' that would be the sum of 'x' +
'y' wihout having to loop?

Something like this (not indeed not working):
S(:).z = S(:).x + S(:).y

With regards,
Phil

Subject: Working with structures

From: Matt J

Date: 13 Jun, 2012 19:21:07

Message: 2 of 3

pmassicotte <pmassicotte@hotmail.com> wrote in message <6ceb3232-70bb-4233-97a5-3551b1c1f3d2@nl1g2000pbc.googlegroups.com>...
>
> Something like this (not indeed not working):
> S(:).z = S(:).x + S(:).y
===============

You would have to do this.

 d=num2cell([S(:).x] + [S(:).y],2);
[S(1:2).z]=deal(d{:});

However, there's little reason not to use a loop. If you're using structs, it probably means the data isn't very large anyway (otherwise it would be a bad idea to use structs).

Subject: Working with structures

From: pmassicotte

Date: 13 Jun, 2012 19:28:27

Message: 3 of 3

On Jun 13, 3:21

Tags for this Thread

No tags are associated with 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