Thread Subject: Assign Vector to Structure Array?

Subject: Assign Vector to Structure Array?

From: jeremy

Date: 18 Jan, 2008 23:43:03

Message: 1 of 10

I would like to know is it possible to assign a vector to a
structure array?

say my vector is x = [1 2 3 4]. can i assign this to

a structure array in a certain field?

So for example, add it to mystruc(x).myfield ?

Subject: Assign Vector to Structure Array?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 19 Jan, 2008 00:16:37

Message: 2 of 10

In article <fmrdi7$sc$1@fred.mathworks.com>,
jeremy <orestes05@hotmail.com> wrote:
>I would like to know is it possible to assign a vector to a
>structure array?

>say my vector is x = [1 2 3 4]. can i assign this to

>a structure array in a certain field?

>So for example, add it to mystruc(x).myfield ?

Are you trying to assign it all to a particular field of a
particular structure array member (e.g., mystruct(5).myfield )
or are you trying to assign consequative members of the field
one at a time to a particular field of consequative structure array
indices (sort of like mystruct(:).myfield ?)


--
  "All is vanity." -- Ecclesiastes

Subject: Assign Vector to Structure Array?

From: jeremy

Date: 19 Jan, 2008 00:35:11

Message: 3 of 10

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fmrfh5$d8$1@canopus.cc.umanitoba.ca>...
> In article <fmrdi7$sc$1@fred.mathworks.com>,
> jeremy <orestes05@hotmail.com> wrote:
> >I would like to know is it possible to assign a vector to a
> >structure array?
>
> >say my vector is x = [1 2 3 4]. can i assign this to
>
> >a structure array in a certain field?
>
> >So for example, add it to mystruc(x).myfield ?
>
> Are you trying to assign it all to a particular field of a
> particular structure array member (e.g.,
mystruct(5).myfield )
> or are you trying to assign consequative members of the field
> one at a time to a particular field of consequative
structure array
> indices (sort of like mystruct(:).myfield ?)
>
>
> --
> "All is vanity." --
Ecclesiastes


I'm sorry that didn't make much sense.

i want to add the same value (e.g. 1) to certain indicies
in an entire field (e.g. mystruct([1 9 99]).myfield = 1)

if that is not possible, is then how to just add the value
to the entire field, so if you access mystruct.myfield, you
get all ones.

Subject: Assign Vector to Structure Array?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 19 Jan, 2008 00:44:12

Message: 4 of 10

In article <fmrgjv$jfq$1@fred.mathworks.com>,
jeremy <orestes05@hotmail.com> wrote:

>i want to add the same value (e.g. 1) to certain indicies
>in an entire field (e.g. mystruct([1 9 99]).myfield = 1)

[mystruct(ismember(1:length(mystruct),[1 9 99]).myfield] = deal(1);

However, doing an addition (e.g. incrementing the present values by 1)
is more difficult; I don't have an expression for that yet.
--
   "No one has the right to destroy another person's belief by
   demanding empirical evidence." -- Ann Landers

Subject: Assign Vector to Structure Array?

From: jeremy

Date: 19 Jan, 2008 03:52:02

Message: 5 of 10

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fmrh4s$2dp$1@canopus.cc.umanitoba.ca>...
> In article <fmrgjv$jfq$1@fred.mathworks.com>,
> jeremy <orestes05@hotmail.com> wrote:
>
> >i want to add the same value (e.g. 1) to certain indicies
> >in an entire field (e.g. mystruct([1 9 99]).myfield = 1)
>
> [mystruct(ismember(1:length(mystruct),[1 9 99]).myfield] =
deal(1);
>
> However, doing an addition (e.g. incrementing the present
values by 1)
> is more difficult; I don't have an expression for that yet.
> --
> "No one has the right to destroy another person's belief by
> demanding empirical evidence." -- Ann
Landers


aahh very good. thats a clever trick. thanks

 

Subject: Assign Vector to Structure Array?

From: Ruben

Date: 2 Feb, 2012 11:10:10

Message: 6 of 10

Hello,

what if I have a array of structures (AS) with various fields (AS.x, AS.y, ...) and I want to assign the values of a vector v= [1 2 3 4] to one of the fields, e.g. to AS.x, in such a way that:

AS(1).x = v(1);
...
AS(end).x = v(end);

I thought deal might help but it doesn't. I also tried with [AS.x] = v but it says "Too many output arguments."

Thank you

Subject: Assign Vector to Structure Array?

From: Bruno Luong

Date: 2 Feb, 2012 11:59:10

Message: 7 of 10

v= [1 2 3 4]
c=num2cell(v)
[AS(1:4).x] = deal(c{:})

% Bruno

Subject: Assign Vector to Structure Array?

From: Ruben

Date: 2 Feb, 2012 13:06:10

Message: 8 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jgdtqe$cc6$1@newscl01ah.mathworks.com>...
> v= [1 2 3 4]
> c=num2cell(v)
> [AS(1:4).x] = deal(c{:})
>
> % Bruno


Thanks Bruno,

that definitely works! I didn't realize I could transform v to a cell array :-)

Cheers

Subject: Assign Vector to Structure Array?

From: Matt J

Date: 2 Feb, 2012 15:21:10

Message: 9 of 10

"Ruben" wrote in message <jgdqui$4fh$1@newscl01ah.mathworks.com>...
> Hello,
>
> what if I have a array of structures (AS) with various fields (AS.x, AS.y, ...) and I want to assign the values of a vector v= [1 2 3 4] to one of the fields, e.g. to AS.x, in such a way that:
>
> AS(1).x = v(1);
> ...
> AS(end).x = v(end);
==================

Note that this is usually a bad idea, however, especially if v is large. To access each v(i), or groups of them, you now not only need more complicated syntax, but you've also scattered them across non-contiguous memory.

Subject: Assign Vector to Structure Array?

From: Ruben

Date: 2 Feb, 2012 15:35:10

Message: 10 of 10


> Note that this is usually a bad idea, however, especially if v is large. To access each v(i), or groups of them, you now not only need more complicated syntax, but you've also scattered them across non-contiguous memory.

I am aware that this scatters the data but it allows me to simplify the code easier. I believe v won't be larger than a couple hundreds of values in most cases.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
assignment Ruben 2 Feb, 2012 06:14:15
struct array Ruben 2 Feb, 2012 06:14:15
rssFeed for this Thread

Contact us at files@mathworks.com