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:
assigning values in structure array

Subject: assigning values in structure array

From: Sargondjani

Date: 7 Feb, 2012 16:04:10

Message: 1 of 9

hi,

im have just started using structure array's, but the notation is a bit hard to understand for me. i looked at everything i found, but still... it seems messy,so i was wondering if somebody could help me out.

i have a 2x3 structure array MS with a some fields

no let's say if want to insert a vector:
v=1:3 as the values in 'field1' for row 1 and w=4:6 in row 2 of the structure, such that MS.field1 has values: [1:3;4:6], so MS(2,2).field1=5

How do i do this? (without using a for loop and oen row after the other)

Thanks alot in advance!! Kind regards,
S.

Any good reference manual on structure arrays would be welcome too (i read alot but none was good enough to understand the subject properly)

Subject: assigning values in structure array

From: Matt J

Date: 7 Feb, 2012 16:14:11

Message: 2 of 9

"Sargondjani" wrote in message <jgri1q$dlg$1@newscl01ah.mathworks.com>...
> hi,
>
> im have just started using structure array's, but the notation is a bit hard to understand for me. i looked at everything i found, but still... it seems messy,so i was wondering if somebody could help me out.
>
> i have a 2x3 structure array MS with a some fields
>
> no let's say if want to insert a vector:
> v=1:3 as the values in 'field1' for row 1 and w=4:6 in row 2 of the structure, such that MS.field1 has values: [1:3;4:6], so MS(2,2).field1=5
>
> How do i do this? (without using a for loop and oen row after the other)
==========

In a variety of ways. None of them will be more efficient than a for-loop, except in syntax. So one way in the case you describe:

temp=num2cell([v;w]);
[MS.filed1]=deal(temp{:});

Subject: assigning values in structure array

From: Sargondjani

Date: 7 Feb, 2012 16:47:09

Message: 3 of 9

Thanks Matt for helping out (again, I think)

Your suggestion makes me understand it more too...

BUt what stilll confuses me is that MS.field1 would give the numbers in a list (going through rows and then the columns). It has to do something with the 'coma separation list'... but how, and most of all, why, is a mystery to me

Subject: assigning values in structure array

From: Sargondjani

Date: 7 Feb, 2012 17:20:10

Message: 4 of 9

Actually that is my biggest concern now: is there a way to view the fields as the 2x3 matrices (as i perceive them to be)

ie. can i get on my screen:

ms.field1=[1 2 3;
               4 5 6]

or something similar (so not a single list that doesnt distinguish between rows and coumns)
i really to want that to keep a 'feeling' with what's going on in the structure

Subject: assigning values in structure array

From: Matt J

Date: 7 Feb, 2012 17:38:10

Message: 5 of 9

"Sargondjani" wrote in message <jgrmg9$2l$1@newscl01ah.mathworks.com>...
> Actually that is my biggest concern now: is there a way to view the fields as the 2x3 matrices (as i perceive them to be)
>
> ie. can i get on my screen:
>
> ms.field1=[1 2 3;
> 4 5 6]
>
> or something similar (so not a single list that doesnt distinguish between rows and coumns)
> i really to want that to keep a 'feeling' with what's going on in the structure

you can do

reshape( [ms.field1], size(ms)),

Subject: assigning values in structure array

From: Sargondjani

Date: 9 Feb, 2012 09:30:18

Message: 6 of 9

thanks!! this made me realize structure arrays are too messy. i'll use a cell array instead

but also there i have problems. i want to assign the value of three cell's simultaneously. is that possible with cell arrays?
the reason i want to do this in one line is to keep a better overview (in my mind x,y,z form a vector)

so i would like to assign
ca.b.l = x
ca.b.m = y
ca.b.h = z

in one line, so something like:
[ca.b.l ca.b.m ca.b.h]=[x, y, z]

but that doesnt work... i tried other options as well, but couldnt find one that works. how should i do this?

Subject: assigning values in structure array

From: Sargondjani

Date: 9 Feb, 2012 09:42:11

Message: 7 of 9

apperently i can solve it using 'deal':

[cb.u.l, cb.u.m, cb.u.h]=deal(x,y,z)

, but isnt there another way?

Subject: assigning values in structure array

From: Matt J

Date: 9 Feb, 2012 14:43:33

Message: 8 of 9

"Sargondjani" wrote in message <jh04dj$orq$1@newscl01ah.mathworks.com>...
> apperently i can solve it using 'deal':
>
> [cb.u.l, cb.u.m, cb.u.h]=deal(x,y,z)
>
> , but isnt there another way?

If l,m,h are the ONLY fields of cb.u then you can do

cb.u=cell2struct({'l','m','h'},num2cell([x,y,z]),2)

However, I'm starting to wonder why you think any of these conversions are going to be helpful to you. If the rectangular structure of the original data is important, why not keep it in its original numeric matrix form, which is after all the most efficient memory-wise and speed-wise?

Subject: assigning values in structure array

From: Sargondjani

Date: 21 Mar, 2012 12:57:12

Message: 9 of 9

> If l,m,h are the ONLY fields of cb.u then you can do
>
> cb.u=cell2struct({'l','m','h'},num2cell([x,y,z]),2)
>
> However, I'm starting to wonder why you think any of these conversions are going to be helpful to you. If the rectangular structure of the original data is important, why not keep it in its original numeric matrix form, which is after all the most efficient memory-wise and speed-wise?

sorry, a bit late, but thanks! l,m,h are the only fields so this should work perfectly!

i use this only to store some parameters for simulations, so speed is not really an issue... also i save simulation results which are more dimensional objects, so a cell array is more appropriate so that everything has the same 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