<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241855</link>
    <title>MATLAB Central Newsreader - Concatenate structure fields with empties</title>
    <description>Feed for thread: Concatenate structure fields with empties</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sat, 03 Jan 2009 17:25:03 -0500</pubDate>
      <title>Concatenate structure fields with empties</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241855#619721</link>
      <author>Sven </author>
      <description>Hi there, just a question about concatenating structure array fields where some of the arrays don't have that field initialised. For example:&lt;br&gt;
&lt;br&gt;
s(1).myField = 100;&lt;br&gt;
s(10).myField = 200;&lt;br&gt;
&lt;br&gt;
[s.myField], or cat(1,s.myfield), both give a result of length 2 since all the other entries are empty.&lt;br&gt;
&lt;br&gt;
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?&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
Cheers,&lt;br&gt;
Sven.</description>
    </item>
    <item>
      <pubDate>Sat, 03 Jan 2009 18:11:38 -0500</pubDate>
      <title>Re: Concatenate structure fields with empties</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241855#619730</link>
      <author>Doug Schwarz</author>
      <description>In article &amp;lt;gjo71f$ct5$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
&amp;nbsp;&quot;Sven&quot; &amp;lt;sven.holcombe@gmail.deleteme.com&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; Hi there, just a question about concatenating structure array fields where &lt;br&gt;
&amp;gt; some of the arrays don't have that field initialised. For example:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; s(1).myField = 100;&lt;br&gt;
&amp;gt; s(10).myField = 200;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [s.myField], or cat(1,s.myfield), both give a result of length 2 since all &lt;br&gt;
&amp;gt; the other entries are empty.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I understand you can't have a 1 by 10 array with elements 2 to 9 being empty. &lt;br&gt;
&amp;gt; How about instead returning those elements as nan?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Basically, what's the most efficient way for me to concatenate these &lt;br&gt;
&amp;gt; structure fields, yet retain the indices where each element was found in the &lt;br&gt;
&amp;gt; structure.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Cheers,&lt;br&gt;
&amp;gt; Sven.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I would say try initializing your structure with NaNs and then overwrite &lt;br&gt;
some of them with your existing code.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;s = struct('myField',num2cell(NaN(1,10)));&lt;br&gt;
&amp;nbsp;&amp;nbsp;s(1).myField = 100;&lt;br&gt;
&amp;nbsp;&amp;nbsp;s(10).myField = 200;&lt;br&gt;
&amp;nbsp;&amp;nbsp;[s.myField]&lt;br&gt;
&amp;nbsp;&amp;nbsp;ans =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;100   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   200&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Doug Schwarz&lt;br&gt;
dmschwarz&amp;ieee,org&lt;br&gt;
Make obvious changes to get real email address.</description>
    </item>
    <item>
      <pubDate>Sun, 04 Jan 2009 18:09:03 -0500</pubDate>
      <title>Re: Concatenate structure fields with empties</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241855#619820</link>
      <author>Sven </author>
      <description>Doug Schwarz &amp;lt;see@sig.for.address.edu&amp;gt; wrote in message &amp;lt;see-C54CAA.13113803012009@news.frontiernet.net&amp;gt;...&lt;br&gt;
&amp;gt; In article &amp;lt;gjo71f$ct5$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
&amp;gt;  &quot;Sven&quot; &amp;lt;sven.holcombe@gmail.deleteme.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Hi there, just a question about concatenating structure array fields where &lt;br&gt;
&amp;gt; &amp;gt; some of the arrays don't have that field initialised. For example:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; s(1).myField = 100;&lt;br&gt;
&amp;gt; &amp;gt; s(10).myField = 200;&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; [s.myField], or cat(1,s.myfield), both give a result of length 2 since all &lt;br&gt;
&amp;gt; &amp;gt; the other entries are empty.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I understand you can't have a 1 by 10 array with elements 2 to 9 being empty. &lt;br&gt;
&amp;gt; &amp;gt; How about instead returning those elements as nan?&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Basically, what's the most efficient way for me to concatenate these &lt;br&gt;
&amp;gt; &amp;gt; structure fields, yet retain the indices where each element was found in the &lt;br&gt;
&amp;gt; &amp;gt; structure.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Cheers,&lt;br&gt;
&amp;gt; &amp;gt; Sven.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I would say try initializing your structure with NaNs and then overwrite &lt;br&gt;
&amp;gt; some of them with your existing code.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   s = struct('myField',num2cell(NaN(1,10)));&lt;br&gt;
&amp;gt;   s(1).myField = 100;&lt;br&gt;
&amp;gt;   s(10).myField = 200;&lt;br&gt;
&amp;gt;   [s.myField]&lt;br&gt;
&amp;gt;   ans =&lt;br&gt;
&amp;gt;      100   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   200&lt;br&gt;
&lt;br&gt;
Thanks Doug, I thought that might be the case. Now perhaps you can help me with a related question....&lt;br&gt;
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:&lt;br&gt;
&lt;br&gt;
emptyIndices = false(1,length(s));&lt;br&gt;
myCell = {s.myField};&lt;br&gt;
for i = 1:length(myCell)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if isempty(myCell{i})&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;emptyIndices(i) = true;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
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?&lt;br&gt;
&lt;br&gt;
Cheers,&lt;br&gt;
Sven.</description>
    </item>
    <item>
      <pubDate>Sun, 04 Jan 2009 19:27:41 -0500</pubDate>
      <title>Re: Concatenate structure fields with empties</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241855#619822</link>
      <author>Doug Schwarz</author>
      <description>In article &amp;lt;gjqtvv$ku0$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
&amp;nbsp;&quot;Sven&quot; &amp;lt;sven.holcombe@gmail.deleteme.com&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; Doug Schwarz &amp;lt;see@sig.for.address.edu&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;lt;see-C54CAA.13113803012009@news.frontiernet.net&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; In article &amp;lt;gjo71f$ct5$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
&amp;gt; &amp;gt;  &quot;Sven&quot; &amp;lt;sven.holcombe@gmail.deleteme.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Hi there, just a question about concatenating structure array fields &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; where &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; some of the arrays don't have that field initialised. For example:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; s(1).myField = 100;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; s(10).myField = 200;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; [s.myField], or cat(1,s.myfield), both give a result of length 2 since &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; all &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; the other entries are empty.&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; I understand you can't have a 1 by 10 array with elements 2 to 9 being &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; empty. &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; How about instead returning those elements as nan?&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Basically, what's the most efficient way for me to concatenate these &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; structure fields, yet retain the indices where each element was found in &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; the &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; structure.&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Cheers,&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Sven.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I would say try initializing your structure with NaNs and then overwrite &lt;br&gt;
&amp;gt; &amp;gt; some of them with your existing code.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;   s = struct('myField',num2cell(NaN(1,10)));&lt;br&gt;
&amp;gt; &amp;gt;   s(1).myField = 100;&lt;br&gt;
&amp;gt; &amp;gt;   s(10).myField = 200;&lt;br&gt;
&amp;gt; &amp;gt;   [s.myField]&lt;br&gt;
&amp;gt; &amp;gt;   ans =&lt;br&gt;
&amp;gt; &amp;gt;      100   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   200&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks Doug, I thought that might be the case. Now perhaps you can help me &lt;br&gt;
&amp;gt; with a related question....&lt;br&gt;
&amp;gt; Let's say that I hadn't initialised with NaNs and wanted to somehow find the &lt;br&gt;
&amp;gt; indices of the empty fields. My somewhat roundabout method is as follows:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; emptyIndices = false(1,length(s));&lt;br&gt;
&amp;gt; myCell = {s.myField};&lt;br&gt;
&amp;gt; for i = 1:length(myCell)&lt;br&gt;
&amp;gt;    if isempty(myCell{i})&lt;br&gt;
&amp;gt;       emptyIndices(i) = true;&lt;br&gt;
&amp;gt;    end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Is there any way to turn this into a 1 or 2-liner?  Ie, can isempty() somehow &lt;br&gt;
&amp;gt; take in a cell array and return whether the *contents* of each cell is empty, &lt;br&gt;
&amp;gt; rather than the cell array itself being empty?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Cheers,&lt;br&gt;
&amp;gt; Sven.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Not by itself, but you can do it with cellfun:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;myCell = {s.myField};&lt;br&gt;
&amp;nbsp;&amp;nbsp;emptyIndices = cellfun(@isempty,myCell);&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Doug Schwarz&lt;br&gt;
dmschwarz&amp;ieee,org&lt;br&gt;
Make obvious changes to get real email address.</description>
    </item>
    <item>
      <pubDate>Sun, 04 Jan 2009 20:01:02 -0500</pubDate>
      <title>Re: Concatenate structure fields with empties</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241855#619825</link>
      <author>Jiro Doke</author>
      <description>&amp;gt; &lt;br&gt;
&amp;gt;   myCell = {s.myField};&lt;br&gt;
&amp;gt;   emptyIndices = cellfun(@isempty,myCell);&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Or&lt;br&gt;
&lt;br&gt;
emptyIndices = arrayfun(@(x) isempty(x.myField), s);&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
jiro</description>
    </item>
    <item>
      <pubDate>Sun, 04 Jan 2009 22:30:04 -0500</pubDate>
      <title>Re: Concatenate structure fields with empties</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241855#619830</link>
      <author>Phil Goddard</author>
      <description>&lt;br&gt;
A somewhat cryptic, and probably inefficient one liner is&lt;br&gt;
result = cellfun(@(in) (1/(1/in)),{s.myField},'ErrorHandler',@(S,D) nan)&lt;br&gt;
&lt;br&gt;
However a loop is probably just as good&lt;br&gt;
for idx = 1:length(s)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;if isempty(s(idx).myField)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s(idx).myField = nan;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
result = [s.myField];&lt;br&gt;
&lt;br&gt;
Phil.</description>
    </item>
    <item>
      <pubDate>Sun, 04 Jan 2009 22:55:03 -0500</pubDate>
      <title>Re: Concatenate structure fields with empties</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/241855#619834</link>
      <author>Jiro Doke</author>
      <description>&amp;gt; &lt;br&gt;
&amp;gt; emptyIndices = arrayfun(@(x) isempty(x.myField), s);&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
You can combine this with deal to place NaN in the empty fields.&lt;br&gt;
&lt;br&gt;
[s(emptyIndices).myField] = deal(NaN);</description>
    </item>
  </channel>
</rss>

