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 assign multiple structure elements at once

Subject: how to assign multiple structure elements at once

From: Fatih

Date: 15 Nov, 2012 18:15:16

Message: 1 of 6

Hi,

I'm trying to assign multiple values to second element of a field of multiple structure elements. Better I give you an example to explain that:

let's say the values are = 1,2,3
structure is = a
field is = s

then the result should look like this:
a(1).s(2) = 1
a(2).s(2) = 2
a(3).s(2) = 3

I need something similar to deal. However with deal function I can only set the first element of field 's' as shown below:

b = num2cell(1:3)
[a(1:3).s] = deal(b{:})
which gives me
a(1).s = 1
a(2).s = 2
a(3).s = 3

Could you please help me?

Subject: how to assign multiple structure elements at once

From: Matt J

Date: 15 Nov, 2012 18:35:20

Message: 2 of 6

"Fatih" wrote in message <k83bfk$1rf$1@newscl01ah.mathworks.com>...
> Hi,
>
> I'm trying to assign multiple values to second element of a field of multiple structure elements. Better I give you an example to explain that:
>
> let's say the values are = 1,2,3
> structure is = a
> field is = s
>
> then the result should look like this:
> a(1).s(2) = 1
> a(2).s(2) = 2
> a(3).s(2) = 3
=================

My answer to posts like these is always to question the way the data is being organized, so I'll do that again here.

Given what you have above, it looks lik all a(i).s are the same size. If some a(j).s is of length 1, for example, this makes no sense. Assuming this to be the case, it probably makes sense for you to make 'a' a scalar struct instead and do

a.s(:,2)=[1;2;3];

Either that, or hold s in a separate array.

Subject: how to assign multiple structure elements at once

From: Fatih

Date: 16 Nov, 2012 11:53:17

Message: 3 of 6

"Matt J" wrote in message <k83cl8$6ft$1@newscl01ah.mathworks.com>...
> "Fatih" wrote in message <k83bfk$1rf$1@newscl01ah.mathworks.com>...
> > Hi,
> >
> > I'm trying to assign multiple values to second element of a field of multiple structure elements. Better I give you an example to explain that:
> >
> > let's say the values are = 1,2,3
> > structure is = a
> > field is = s
> >
> > then the result should look like this:
> > a(1).s(2) = 1
> > a(2).s(2) = 2
> > a(3).s(2) = 3
> =================
>
> My answer to posts like these is always to question the way the data is being organized, so I'll do that again here.
>
> Given what you have above, it looks lik all a(i).s are the same size. If some a(j).s is of length 1, for example, this makes no sense. Assuming this to be the case, it probably makes sense for you to make 'a' a scalar struct instead and do
>
> a.s(:,2)=[1;2;3];
>
> Either that, or hold s in a separate array.

Hi Mat,
Thanks for your post, however that does not help me much and the reason is the structure organization should not be like the one you gave with your post.
Say I have 10 buses (a) operating daily in a route and I am sampling every hour the number of passengers (s) riding each of those buses. Some of those buses can be broken at any sampling time, and I am only recording that data for good buses. So if that bus does not work any more the number of passengers field will not be updated anymore.
So by looking at this example a.s(:,2) = [1;2;3] does not work. Maybe I should change the organization or just use a for loop.
I was thinking there can be a trick like deal function that fits here nicely.

Subject: how to assign multiple structure elements at once

From: Matt J

Date: 16 Nov, 2012 12:54:11

Message: 4 of 6

"Fatih" wrote in message <k859fd$1fr$1@newscl01ah.mathworks.com>...
>
> Hi Mat,
> Thanks for your post, however that does not help me much and the reason is the structure organization should not be like the one you gave with your post.
> Say I have 10 buses (a) operating daily in a route and I am sampling every hour the number of passengers (s) riding each of those buses. Some of those buses can be broken at any sampling time, and I am only recording that data for good buses. So if that bus does not work any more the number of passengers field will not be updated anymore.
> So by looking at this example a.s(:,2) = [1;2;3] does not work. Maybe I should change the organization or just use a for loop.
> I was thinking there can be a trick like deal function that fits here nicely.
================

It sounds preferable to use a 10xN matrix where N is the number of sampling times to record the data and put it in NaN at hours where a particular bus is not working. Not only is this much easier to index/access, but notice also that that would ensure that your samples are always synchronized if a bus breaks for some period of samples and then starts working again. That's much harder to do if you simply don't record the activity of broken buses.

Subject: how to assign multiple structure elements at once

From: Fatih

Date: 17 Nov, 2012 08:38:13

Message: 5 of 6

> It sounds preferable to use a 10xN matrix where N is the number of sampling times to record the data and put it in NaN at hours where a particular bus is not working. Not only is this much easier to index/access, but notice also that that would ensure that your samples are always synchronized if a bus breaks for some period of samples and then starts working again. That's much harder to do if you simply don't record the activity of broken buses.
===================================================

Thanks for your help. It looks like it is better to change the whole data structure to make it easier and faster the recording process. But it would be nice to have this functionality to simultenously record a data to structure array like the way I was trying to implement. Because in my real case I have thousands of buses and tens of fields to fill in data, over time as the matrices become huge the memory fails to process that data.

Subject: How to ADD additional 'coins' on one image that you copy

From: Jean Marc

Date: 17 Nov, 2012 15:01:18

Message: 6 of 6

"Fatih" wrote in message <k87idl$h5o$1@newscl01ah.mathworks.com>...
If I copy J=imread('eight.tif') image, what should i do to add additional coins on that image
> > > ===================================================

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