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:
how to sum all values in a structure

Subject: how to sum all values in a structure

From: Bruce Heu

Date: 19 Jul, 2012 15:31:08

Message: 1 of 2

A.a = 1
A.b.c=2
A.b.d=3
A.v=3
A.e.r.s=2
A.e.r.t=3

For same length structure, I know we can use command like sum(cat(1,A.b)) = 5 or
sum(cat(1,A.e.r)) = 5. sum(cell2mat(struct2cell(A.b)),1) will work also.

How to add them all together? Like
A.a + A.b.c + A.b.d + A.v + A.e.r.s + A.e.r.t = 14
using some simple command like sum(A) to add all children values under A together.

Thanks a lot

flyingcow

Subject: how to sum all values in a structure

From: Matt J

Date: 19 Jul, 2012 15:42:54

Message: 2 of 2

"Bruce Heu" wrote in message <ju997s$oq3$1@newscl01ah.mathworks.com>...
> A.a = 1
> A.b.c=2
> A.b.d=3
> A.v=3
> A.e.r.s=2
> A.e.r.t=3
>
> For same length structure, I know we can use command like sum(cat(1,A.b)) = 5 or
> sum(cat(1,A.e.r)) = 5. sum(cell2mat(struct2cell(A.b)),1) will work also.
>
> How to add them all together? Like
> A.a + A.b.c + A.b.d + A.v + A.e.r.s + A.e.r.t = 14
> using some simple command like sum(A) to add all children values under A together.
===============

There's no "simple" way. If you need to do this, it probably means you've stored your data in the wrong way. You should be putting your data in a numeric array, rather than a struct, if you want to add them all together.

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