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:
Versatile way to access _next_ element in array

Subject: Versatile way to access _next_ element in array

From: Misha Koshelev

Date: 28 Jul, 2009 23:24:02

Message: 1 of 8

I am trying to access
a(end+1)
where if the array is empty it will default to
a(1)

I get an error when I do a(end+1) on an empty array a.
(These are structure arrays).

Is there a good syntax to do this? In other words I want to type:

a(blah) = b;

where blah will either:
1. create array a if none exists and set first element to b
or
2. append b to a

Thank you
Misha

Subject: Versatile way to access _next_ element in array

From: Matt Fig

Date: 28 Jul, 2009 23:59:02

Message: 2 of 8

help mod

Subject: Versatile way to access _next_ element in array

From: Doug Schwarz

Date: 29 Jul, 2009 00:04:27

Message: 3 of 8

In article <h4o1ai$og6$1@fred.mathworks.com>,
 "Misha Koshelev" <mk144210@bcm.edu> wrote:

> I am trying to access
> a(end+1)
> where if the array is empty it will default to
> a(1)
>
> I get an error when I do a(end+1) on an empty array a.
> (These are structure arrays).
>
> Is there a good syntax to do this? In other words I want to type:
>
> a(blah) = b;
>
> where blah will either:
> 1. create array a if none exists and set first element to b
> or
> 2. append b to a
>
> Thank you
> Misha

You can do

  a = [];
  a(end+1) = b;

but there is no statement

  a(blah)

that will work if the variable a doesn't already exist.

Since you said a is a structure array it's even worse than that. a must
be a structure with the same fields as b:

  b = struct('x',1,'y',2);
  a = struct('x',{},'y',{});
  a(end+1) = b;

will work and that's about as simple as I can make it.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Versatile way to access _next_ element in array

From: Misha Koshelev

Date: 29 Jul, 2009 12:31:03

Message: 4 of 8

Doug Schwarz <see@sig.for.address.edu> wrote in message <see-C69A13.20042628072009@news.frontiernet.net>...
> In article <h4o1ai$og6$1@fred.mathworks.com>,
> "Misha Koshelev" <mk144210@bcm.edu> wrote:
>
> > I am trying to access
> > a(end+1)
> > where if the array is empty it will default to
> > a(1)
> >
> > I get an error when I do a(end+1) on an empty array a.
> > (These are structure arrays).
> >
> > Is there a good syntax to do this? In other words I want to type:
> >
> > a(blah) = b;
> >
> > where blah will either:
> > 1. create array a if none exists and set first element to b
> > or
> > 2. append b to a
> >
> > Thank you
> > Misha
>
> You can do
>
> a = [];
> a(end+1) = b;
>
> but there is no statement
>
> a(blah)
>
> that will work if the variable a doesn't already exist.
>
> Since you said a is a structure array it's even worse than that. a must
> be a structure with the same fields as b:
>
> b = struct('x',1,'y',2);
> a = struct('x',{},'y',{});
> a(end+1) = b;
>
> will work and that's about as simple as I can make it.
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Thanks. The other problem with doing the a = struct, a(end+1) is that it will add and make a _second_ element.

What I'd really like is:
1. If a is empty, make the value first element.
2. If a is not empty, make the value the next element.

In my mind this is the meaning of "append" in English.

Thank you
Misha

Subject: Versatile way to access _next_ element in array

From: Bruno Luong

Date: 29 Jul, 2009 13:20:04

Message: 5 of 8

"Misha Koshelev" <mk144210@bcm.edu> wrote in message <h4pfe7$7mf$1@fred.mathworks.com>...
> Doug Schwarz <see@sig.for.address.edu> wrote in message <see-C69A13.20042628072009@news.frontiernet.net>...
> > In article <h4o1ai$og6$1@fred.mathworks.com>,
> > "Misha Koshelev" <mk144210@bcm.edu> wrote:
> >
> > > I am trying to access
> > > a(end+1)
> > > where if the array is empty it will default to
> > > a(1)
> > >
> > > I get an error when I do a(end+1) on an empty array a.
> > > (These are structure arrays).
> > >
> > > Is there a good syntax to do this? In other words I want to type:
> > >
> > > a(blah) = b;
> > >
> > > where blah will either:
> > > 1. create array a if none exists and set first element to b
> > > or
> > > 2. append b to a
> > >
> > > Thank you
> > > Misha
> >
> > You can do
> >
> > a = [];
> > a(end+1) = b;
> >
> > but there is no statement
> >
> > a(blah)
> >
> > that will work if the variable a doesn't already exist.
> >
> > Since you said a is a structure array it's even worse than that. a must
> > be a structure with the same fields as b:
> >
> > b = struct('x',1,'y',2);
> > a = struct('x',{},'y',{});
> > a(end+1) = b;
> >
> > will work and that's about as simple as I can make it.
> >
> > --
> > Doug Schwarz
> > dmschwarz&ieee,org
> > Make obvious changes to get real email address.
>
> Thanks. The other problem with doing the a = struct, a(end+1) is that it will add and make a _second_ element.

That's not right. a = struct('x',{},'y',{}); returns a 0x0 structure; Appending will place the new structure at the _first_ position.

Subject: Versatile way to access _next_ element in array

From: Andy

Date: 29 Jul, 2009 13:32:02

Message: 6 of 8

"Misha Koshelev" <mk144210@bcm.edu> wrote in message <h4o1ai$og6$1@fred.mathworks.com>...
> I am trying to access
> a(end+1)
> where if the array is empty it will default to
> a(1)
>
> I get an error when I do a(end+1) on an empty array a.
> (These are structure arrays).
>
> Is there a good syntax to do this? In other words I want to type:
>
> a(blah) = b;
>
> where blah will either:
> 1. create array a if none exists and set first element to b
> or
> 2. append b to a
>
> Thank you
> Misha

So you want an array of structures with a special function (method) to append? It sounds like you should create a class.

Subject: Versatile way to access _next_ element in array

From: Misha Koshelev

Date: 29 Jul, 2009 16:58:02

Message: 7 of 8

"Andy " <theorigamist@gmail.com> wrote in message <h4pj0i$qku$1@fred.mathworks.com>...
> "Misha Koshelev" <mk144210@bcm.edu> wrote in message <h4o1ai$og6$1@fred.mathworks.com>...
> > I am trying to access
> > a(end+1)
> > where if the array is empty it will default to
> > a(1)
> >
> > I get an error when I do a(end+1) on an empty array a.
> > (These are structure arrays).
> >
> > Is there a good syntax to do this? In other words I want to type:
> >
> > a(blah) = b;
> >
> > where blah will either:
> > 1. create array a if none exists and set first element to b
> > or
> > 2. append b to a
> >
> > Thank you
> > Misha
>
> So you want an array of structures with a special function (method) to append? It sounds like you should create a class.

Thanks, here's what I did. Seems to work for my purposes.

Misha

function new_voxels = append_source(voxels, source)
% APPEND_SOURCE Append a source to a Voxel2 voxels structure.
% NEW_VOXELS = APPEND_SOURCE(VOXELS, SOURCE) If no source structure
% exists or it is empty, the structure is created with source
% SOURCE. Otherwise, the source is appended.
%
% Misha Koshelev
% July 29, 2009
% Montague Laboratory
    
    if ~isfield(voxels, 'source') || ...
            isempty(voxels.source)
        voxels.source = source;
    
    else
        % New source
        idx_source = length(voxels.source)+1;
        
        % Field names in new source
        fields_source = fieldnames(source);
        for idx_field_source=1:length(fields_source)
            
            field = fields_source{idx_field_source};
            voxels.source(idx_source).(field) = source.(field);
        end
    end
    
    new_voxels = voxels;

Subject: Versatile way to access _next_ element in array

From: Misha Koshelev

Date: 29 Jul, 2009 17:00:03

Message: 8 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h4pia4$bk1$1@fred.mathworks.com>...
> "Misha Koshelev" <mk144210@bcm.edu> wrote in message <h4pfe7$7mf$1@fred.mathworks.com>...
> > Doug Schwarz <see@sig.for.address.edu> wrote in message <see-C69A13.20042628072009@news.frontiernet.net>...
> > > In article <h4o1ai$og6$1@fred.mathworks.com>,
> > > "Misha Koshelev" <mk144210@bcm.edu> wrote:
> > >
> > > > I am trying to access
> > > > a(end+1)
> > > > where if the array is empty it will default to
> > > > a(1)
> > > >
> > > > I get an error when I do a(end+1) on an empty array a.
> > > > (These are structure arrays).
> > > >
> > > > Is there a good syntax to do this? In other words I want to type:
> > > >
> > > > a(blah) = b;
> > > >
> > > > where blah will either:
> > > > 1. create array a if none exists and set first element to b
> > > > or
> > > > 2. append b to a
> > > >
> > > > Thank you
> > > > Misha
> > >
> > > You can do
> > >
> > > a = [];
> > > a(end+1) = b;
> > >
> > > but there is no statement
> > >
> > > a(blah)
> > >
> > > that will work if the variable a doesn't already exist.
> > >
> > > Since you said a is a structure array it's even worse than that. a must
> > > be a structure with the same fields as b:
> > >
> > > b = struct('x',1,'y',2);
> > > a = struct('x',{},'y',{});
> > > a(end+1) = b;
> > >
> > > will work and that's about as simple as I can make it.
> > >
> > > --
> > > Doug Schwarz
> > > dmschwarz&ieee,org
> > > Make obvious changes to get real email address.
> >
> > Thanks. The other problem with doing the a = struct, a(end+1) is that it will add and make a _second_ element.
>
> That's not right. a = struct('x',{},'y',{}); returns a 0x0 structure; Appending will place the new structure at the _first_ position.

You are right btw. I was doing a = struct(); which returns a 1x1 structure. Anyway solved with special function. Thank you.

Any ideas btw on my legend question? Its quite annoying.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/257204#668944

Thank you
Misha

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