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:
Concatenate structure fields with empties

Subject: Concatenate structure fields with empties

From: Sven

Date: 3 Jan, 2009 17:25:03

Message: 1 of 7

Hi there, just a question about concatenating structure array fields where some of the arrays don't have that field initialised. For example:

s(1).myField = 100;
s(10).myField = 200;

[s.myField], or cat(1,s.myfield), both give a result of length 2 since all the other entries are empty.

I understand you can't have a 1 by 10 array with elements 2 to 9 being empty. How about instead returning those elements as nan?

Basically, what's the most efficient way for me to concatenate these structure fields, yet retain the indices where each element was found in the structure.

Cheers,
Sven.

Subject: Concatenate structure fields with empties

From: Doug Schwarz

Date: 3 Jan, 2009 18:11:38

Message: 2 of 7

In article <gjo71f$ct5$1@fred.mathworks.com>,
 "Sven" <sven.holcombe@gmail.deleteme.com> wrote:

> Hi there, just a question about concatenating structure array fields where
> some of the arrays don't have that field initialised. For example:
>
> s(1).myField = 100;
> s(10).myField = 200;
>
> [s.myField], or cat(1,s.myfield), both give a result of length 2 since all
> the other entries are empty.
>
> I understand you can't have a 1 by 10 array with elements 2 to 9 being empty.
> How about instead returning those elements as nan?
>
> Basically, what's the most efficient way for me to concatenate these
> structure fields, yet retain the indices where each element was found in the
> structure.
>
> Cheers,
> Sven.


I would say try initializing your structure with NaNs and then overwrite
some of them with your existing code.

  s = struct('myField',num2cell(NaN(1,10)));
  s(1).myField = 100;
  s(10).myField = 200;
  [s.myField]
  ans =
     100 NaN NaN NaN NaN NaN NaN NaN NaN 200

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

Subject: Concatenate structure fields with empties

From: Sven

Date: 4 Jan, 2009 18:09:03

Message: 3 of 7

Doug Schwarz <see@sig.for.address.edu> wrote in message <see-C54CAA.13113803012009@news.frontiernet.net>...
> In article <gjo71f$ct5$1@fred.mathworks.com>,
> "Sven" <sven.holcombe@gmail.deleteme.com> wrote:
>
> > Hi there, just a question about concatenating structure array fields where
> > some of the arrays don't have that field initialised. For example:
> >
> > s(1).myField = 100;
> > s(10).myField = 200;
> >
> > [s.myField], or cat(1,s.myfield), both give a result of length 2 since all
> > the other entries are empty.
> >
> > I understand you can't have a 1 by 10 array with elements 2 to 9 being empty.
> > How about instead returning those elements as nan?
> >
> > Basically, what's the most efficient way for me to concatenate these
> > structure fields, yet retain the indices where each element was found in the
> > structure.
> >
> > Cheers,
> > Sven.
>
>
> I would say try initializing your structure with NaNs and then overwrite
> some of them with your existing code.
>
> s = struct('myField',num2cell(NaN(1,10)));
> s(1).myField = 100;
> s(10).myField = 200;
> [s.myField]
> ans =
> 100 NaN NaN NaN NaN NaN NaN NaN NaN 200

Thanks Doug, I thought that might be the case. Now perhaps you can help me with a related question....
Let's say that I hadn't initialised with NaNs and wanted to somehow find the indices of the empty fields. My somewhat roundabout method is as follows:

emptyIndices = false(1,length(s));
myCell = {s.myField};
for i = 1:length(myCell)
   if isempty(myCell{i})
      emptyIndices(i) = true;
   end
end

Is there any way to turn this into a 1 or 2-liner? Ie, can isempty() somehow take in a cell array and return whether the *contents* of each cell is empty, rather than the cell array itself being empty?

Cheers,
Sven.

Subject: Concatenate structure fields with empties

From: Doug Schwarz

Date: 4 Jan, 2009 19:27:41

Message: 4 of 7

In article <gjqtvv$ku0$1@fred.mathworks.com>,
 "Sven" <sven.holcombe@gmail.deleteme.com> wrote:

> Doug Schwarz <see@sig.for.address.edu> wrote in message
> <see-C54CAA.13113803012009@news.frontiernet.net>...
> > In article <gjo71f$ct5$1@fred.mathworks.com>,
> > "Sven" <sven.holcombe@gmail.deleteme.com> wrote:
> >
> > > Hi there, just a question about concatenating structure array fields
> > > where
> > > some of the arrays don't have that field initialised. For example:
> > >
> > > s(1).myField = 100;
> > > s(10).myField = 200;
> > >
> > > [s.myField], or cat(1,s.myfield), both give a result of length 2 since
> > > all
> > > the other entries are empty.
> > >
> > > I understand you can't have a 1 by 10 array with elements 2 to 9 being
> > > empty.
> > > How about instead returning those elements as nan?
> > >
> > > Basically, what's the most efficient way for me to concatenate these
> > > structure fields, yet retain the indices where each element was found in
> > > the
> > > structure.
> > >
> > > Cheers,
> > > Sven.
> >
> >
> > I would say try initializing your structure with NaNs and then overwrite
> > some of them with your existing code.
> >
> > s = struct('myField',num2cell(NaN(1,10)));
> > s(1).myField = 100;
> > s(10).myField = 200;
> > [s.myField]
> > ans =
> > 100 NaN NaN NaN NaN NaN NaN NaN NaN 200
>
> Thanks Doug, I thought that might be the case. Now perhaps you can help me
> with a related question....
> Let's say that I hadn't initialised with NaNs and wanted to somehow find the
> indices of the empty fields. My somewhat roundabout method is as follows:
>
> emptyIndices = false(1,length(s));
> myCell = {s.myField};
> for i = 1:length(myCell)
> if isempty(myCell{i})
> emptyIndices(i) = true;
> end
> end
>
> Is there any way to turn this into a 1 or 2-liner? Ie, can isempty() somehow
> take in a cell array and return whether the *contents* of each cell is empty,
> rather than the cell array itself being empty?
>
> Cheers,
> Sven.


Not by itself, but you can do it with cellfun:

  myCell = {s.myField};
  emptyIndices = cellfun(@isempty,myCell);

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

Subject: Concatenate structure fields with empties

From: Jiro Doke

Date: 4 Jan, 2009 20:01:02

Message: 5 of 7

>
> myCell = {s.myField};
> emptyIndices = cellfun(@isempty,myCell);
>

Or

emptyIndices = arrayfun(@(x) isempty(x.myField), s);




jiro

Subject: Concatenate structure fields with empties

From: Phil Goddard

Date: 4 Jan, 2009 22:30:04

Message: 6 of 7


A somewhat cryptic, and probably inefficient one liner is
result = cellfun(@(in) (1/(1/in)),{s.myField},'ErrorHandler',@(S,D) nan)

However a loop is probably just as good
for idx = 1:length(s)
   if isempty(s(idx).myField)
      s(idx).myField = nan;
   end
end
result = [s.myField];

Phil.

Subject: Concatenate structure fields with empties

From: Jiro Doke

Date: 4 Jan, 2009 22:55:03

Message: 7 of 7

>
> emptyIndices = arrayfun(@(x) isempty(x.myField), s);
>

You can combine this with deal to place NaN in the empty fields.

[s(emptyIndices).myField] = deal(NaN);

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