<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: MATLAB Basics: Array of structures vs Structures of arrays</title>
	<atom:link href="http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/</link>
	<description>&#60;a href=&#34;http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objectId=1093599&#38;objectType=author&#34;&#62;Brett&#60;/a&#62; &#38; &#60;a href=&#34;http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objectId=1094142&#38;objectType=author&#34;&#62;Jiro&#60;/a&#62; share favorite user-contributed submissions from the File Exchange.</description>
	<lastBuildDate>Tue, 30 Apr 2013 09:18:26 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Greg Stupp</title>
		<link>http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-17068</link>
		<dc:creator>Greg Stupp</dc:creator>
		<pubDate>Wed, 12 Dec 2012 20:44:19 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-17068</guid>
		<description>This may be helpful to someone. I couldn&#039;t find a built-in function or anything in the file exchange that does this.


function out=SoA2AoS(in)
%convert structure of arrays to an array of structures
%the lengths of the fields must be the same

% example:
% in.a=[1:5];
% in.b=[11:15];
% out=SoA2AoS(in)
% out(1)

fn=fieldnames(in);
c=struct2cell(in);

for i=1:length(fn)
    d=num2cell(c{i});
    m=num2str(length(d));
    eval([&#039;[out(1:&#039; m &#039;).&#039; fn{i} &#039;]=deal(d{:});&#039;])
end
</description>
		<content:encoded><![CDATA[<p>This may be helpful to someone. I couldn&#8217;t find a built-in function or anything in the file exchange that does this.</p>
<p>function out=SoA2AoS(in)<br />
%convert structure of arrays to an array of structures<br />
%the lengths of the fields must be the same</p>
<p>% example:<br />
% in.a=[1:5];<br />
% in.b=[11:15];<br />
% out=SoA2AoS(in)<br />
% out(1)</p>
<p>fn=fieldnames(in);<br />
c=struct2cell(in);</p>
<p>for i=1:length(fn)<br />
    d=num2cell(c{i});<br />
    m=num2str(length(d));<br />
    eval(['[out(1:' m ').' fn{i} ']=deal(d{:});&#8217;])<br />
end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Asit</title>
		<link>http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14570</link>
		<dc:creator>Asit</dc:creator>
		<pubDate>Wed, 20 Jun 2012 06:14:54 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14570</guid>
		<description>Hi.
   while populating an array of structures, can I use such a syntax as 
           data(3:10).pin = [6 7];
I am getting an error while trying to use this. I hope it is clear as to what I want to achieve. Please let me know if there is a work around.

Thanks,
a.</description>
		<content:encoded><![CDATA[<p>Hi.<br />
   while populating an array of structures, can I use such a syntax as<br />
           data(3:10).pin = [6 7];<br />
I am getting an error while trying to use this. I hope it is clear as to what I want to achieve. Please let me know if there is a work around.</p>
<p>Thanks,<br />
a.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shaheen Iqubal</title>
		<link>http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14520</link>
		<dc:creator>Shaheen Iqubal</dc:creator>
		<pubDate>Tue, 08 May 2012 21:21:01 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14520</guid>
		<description>Hi,
I am trying to merge two array of structure based on the common field security_ID. The last two lines does not seem to work. I want to assign the Return and ReturnDt column from T2 array into T1 array. It is possible that some security_id that are in T1 are not in T2 and vice-versa. I can do the assignment in the loop or use arrayfun but trying to do in one line of code.


    ID1=[T1.Security_ID];
    ID2=[T2.Security_ID];
    [a,b,c]=intersect(ID1,ID2);
    T1(b).Return=[T2(c).Return];
    T1(b).ReturnDt=[T2(c).ReturnDt];</description>
		<content:encoded><![CDATA[<p>Hi,<br />
I am trying to merge two array of structure based on the common field security_ID. The last two lines does not seem to work. I want to assign the Return and ReturnDt column from T2 array into T1 array. It is possible that some security_id that are in T1 are not in T2 and vice-versa. I can do the assignment in the loop or use arrayfun but trying to do in one line of code.</p>
<p>    ID1=[T1.Security_ID];<br />
    ID2=[T2.Security_ID];<br />
    [a,b,c]=intersect(ID1,ID2);<br />
    T1(b).Return=[T2(c).Return];<br />
    T1(b).ReturnDt=[T2(c).ReturnDt];</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tavi</title>
		<link>http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14454</link>
		<dc:creator>tavi</dc:creator>
		<pubDate>Mon, 19 Mar 2012 13:25:30 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14454</guid>
		<description>For example my parameters should be initialize like this: in the first position of my structure I have a number, in the second position a number, in the third position a vector and so on till the 9th position.

Regards,
Octavian</description>
		<content:encoded><![CDATA[<p>For example my parameters should be initialize like this: in the first position of my structure I have a number, in the second position a number, in the third position a vector and so on till the 9th position.</p>
<p>Regards,<br />
Octavian</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tavi</title>
		<link>http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14453</link>
		<dc:creator>tavi</dc:creator>
		<pubDate>Mon, 19 Mar 2012 13:20:24 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14453</guid>
		<description>I have a question. How do I create a structure in order to initialize some parameters needed in other functions? If someone is interested in answering this question I will write him a part of the code.

Regards,
Octavian</description>
		<content:encoded><![CDATA[<p>I have a question. How do I create a structure in order to initialize some parameters needed in other functions? If someone is interested in answering this question I will write him a part of the code.</p>
<p>Regards,<br />
Octavian</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: F. Ojeda</title>
		<link>http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14432</link>
		<dc:creator>F. Ojeda</dc:creator>
		<pubDate>Thu, 23 Feb 2012 16:00:21 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14432</guid>
		<description>Hi I&#039;ve been looking through your post but couldn&#039;t find and answer to my problem, which resides in the &quot;array of structures&quot;- &quot;structure of arrays&quot; paradigm 

I have the following text file containing exchange rates,  

id code rate
1 Euro 1
2 HUF 0.0036
3 PZL 0.2532
4 CZK 0.0406
5 SKK 0.03319
6 BGN 0.5113

which I want to import into an &quot;array of structures&quot;, of the form

currency(1).id = 1
currency(1).code = &#039;Euro&#039;
currency(1).rate = 1
.
.
.
currency(6).id = 6
currency(6).code = &#039;BGN&#039;
currency(6).rate = 0.5113

Of course I&#039;d like to avoid this manual work with for loops and instead create dynamicaly the structure fields using the file headers and populating the structure.

My immediate approach was first to read the file with textscan
 
fid = fopen(&#039;file.txt&#039;)
names = textscan(fid,&#039;%s %s %s&#039;,1,&#039;CollectOutput&#039;,1);
values = textscan(fid,&#039;%s %s %s&#039;,&#039;CollectOutput&#039;,1);
fclose(fid)

thus generating two cell arrays. Secondly, I use the cell2struct function as

currency = cell2struct(content,headers{:},2)

which creates a &quot;structure of arrays&quot;

currency = 
          id: [6x1 uint32]
          code: 1x1 cell array
          rate: [6x1 double]

The indexing is the of the form 

currency.id(1)
currency.code{1}

How could I then generate using the same reasoning an array of structures?

thanks</description>
		<content:encoded><![CDATA[<p>Hi I&#8217;ve been looking through your post but couldn&#8217;t find and answer to my problem, which resides in the &#8220;array of structures&#8221;- &#8220;structure of arrays&#8221; paradigm </p>
<p>I have the following text file containing exchange rates,  </p>
<p>id code rate<br />
1 Euro 1<br />
2 HUF 0.0036<br />
3 PZL 0.2532<br />
4 CZK 0.0406<br />
5 SKK 0.03319<br />
6 BGN 0.5113</p>
<p>which I want to import into an &#8220;array of structures&#8221;, of the form</p>
<p>currency(1).id = 1<br />
currency(1).code = &#8216;Euro&#8217;<br />
currency(1).rate = 1<br />
.<br />
.<br />
.<br />
currency(6).id = 6<br />
currency(6).code = &#8216;BGN&#8217;<br />
currency(6).rate = 0.5113</p>
<p>Of course I&#8217;d like to avoid this manual work with for loops and instead create dynamicaly the structure fields using the file headers and populating the structure.</p>
<p>My immediate approach was first to read the file with textscan</p>
<p>fid = fopen(&#8216;file.txt&#8217;)<br />
names = textscan(fid,&#8217;%s %s %s&#8217;,1,&#8217;CollectOutput&#8217;,1);<br />
values = textscan(fid,&#8217;%s %s %s&#8217;,'CollectOutput&#8217;,1);<br />
fclose(fid)</p>
<p>thus generating two cell arrays. Secondly, I use the cell2struct function as</p>
<p>currency = cell2struct(content,headers{:},2)</p>
<p>which creates a &#8220;structure of arrays&#8221;</p>
<p>currency =<br />
          id: [6x1 uint32]<br />
          code: 1&#215;1 cell array<br />
          rate: [6x1 double]</p>
<p>The indexing is the of the form </p>
<p>currency.id(1)<br />
currency.code{1}</p>
<p>How could I then generate using the same reasoning an array of structures?</p>
<p>thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Torek</title>
		<link>http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14371</link>
		<dc:creator>Paul Torek</dc:creator>
		<pubDate>Mon, 19 Dec 2011 20:45:24 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14371</guid>
		<description>Thankyou thankyou thankyou!

You gave me the direction I needed to extract relevant info from the matlab &lt;i&gt;dir filename&lt;/i&gt; command.  Such as:


function Subdirs = OneLevelSubDirs(directory,match)
% xxx Need Help xxx
% Subdirs = OneLevelSubDirs(directory,match)

if nargin &lt; 2 &#124;&#124; isempty(match)
    match = &#039;*&#039;;
end

X = dir(directory);

Xdir = {X(:).name};
Xdir = Xdir(find(cell2mat({X(:).isdir})));
Subdirs = Xdir(~cellfun(@(x)ismember(x,{&#039;.&#039;;&#039;..&#039;}),Xdir));


Did I mention thank you?</description>
		<content:encoded><![CDATA[<p>Thankyou thankyou thankyou!</p>
<p>You gave me the direction I needed to extract relevant info from the matlab <i>dir filename</i> command.  Such as:</p>
<p>function Subdirs = OneLevelSubDirs(directory,match)<br />
% xxx Need Help xxx<br />
% Subdirs = OneLevelSubDirs(directory,match)</p>
<p>if nargin &lt; 2 || isempty(match)<br />
    match = &#039;*&#039;;<br />
end</p>
<p>X = dir(directory);</p>
<p>Xdir = {X(:).name};<br />
Xdir = Xdir(find(cell2mat({X(:).isdir})));<br />
Subdirs = Xdir(~cellfun(@(x)ismember(x,{&#039;.&#039;;&#039;..&#039;}),Xdir));</p>
<p>Did I mention thank you?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nick</title>
		<link>http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14271</link>
		<dc:creator>nick</dc:creator>
		<pubDate>Thu, 13 Oct 2011 15:43:16 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14271</guid>
		<description>hi
i am new in MATLAB, can u plz tell whats d problem with the following statements (i need to assign an array of integers to a member of an array of structures )
    n(1).loc=[x-1 y+1];
    n(2).loc=[x y+1];
    n(3).loc=[x+1 y+1];
    n(4).loc=[x+1 y];
    n(5).loc=[x+1 y-1];
why are these assignments wrong?? plz reply ASAP... thnx</description>
		<content:encoded><![CDATA[<p>hi<br />
i am new in MATLAB, can u plz tell whats d problem with the following statements (i need to assign an array of integers to a member of an array of structures )<br />
    n(1).loc=[x-1 y+1];<br />
    n(2).loc=[x y+1];<br />
    n(3).loc=[x+1 y+1];<br />
    n(4).loc=[x+1 y];<br />
    n(5).loc=[x+1 y-1];<br />
why are these assignments wrong?? plz reply ASAP&#8230; thnx</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Omar</title>
		<link>http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14249</link>
		<dc:creator>Omar</dc:creator>
		<pubDate>Tue, 27 Sep 2011 21:36:07 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14249</guid>
		<description>I want to have a .wav file into a vector so I can create de Spectra with the SPtool.

When I use the wavread() function and  import the data into sptool it say thats an [array] so i couldnt create the spectrum, this tool only work with vectors. So how can i turn this array into vector? 

a little help here please</description>
		<content:encoded><![CDATA[<p>I want to have a .wav file into a vector so I can create de Spectra with the SPtool.</p>
<p>When I use the wavread() function and  import the data into sptool it say thats an [array] so i couldnt create the spectrum, this tool only work with vectors. So how can i turn this array into vector? </p>
<p>a little help here please</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve L</title>
		<link>http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14217</link>
		<dc:creator>Steve L</dc:creator>
		<pubDate>Fri, 19 Aug 2011 13:16:45 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/pick/2008/04/22/matlab-basics-array-of-structures-vs-structures-of-arrays/#comment-14217</guid>
		<description>Annon,

To do this you can use DEAL, but I think Doug&#039;s question is a good one: why do you need/want to do it this way?

&lt;pre&gt;
% Make sure people2 doesn&#039;t exist yet
% This way you can tell that the command performed
% the initial creation correctly
clear people2

% Define the cell array of names
S = {&#039;bob&#039;, &#039;john&#039;};

% DEAL the elements of the cell into the struct
[people2(1:2).name] = deal(S{:})
&lt;/pre&gt;

If you&#039;re using release R14 or later, you can omit the DEAL as described in the &lt;a href=&quot;http://www.mathworks.com/help/techdoc/rn/f8-1009921.html#f8-1013499&quot; rel=&quot;nofollow&quot;&gt;Release Notes&lt;/a&gt;.</description>
		<content:encoded><![CDATA[<p>Annon,</p>
<p>To do this you can use DEAL, but I think Doug&#8217;s question is a good one: why do you need/want to do it this way?</p>
<pre>
% Make sure people2 doesn't exist yet
% This way you can tell that the command performed
% the initial creation correctly
clear people2

% Define the cell array of names
S = {'bob', 'john'};

% DEAL the elements of the cell into the struct
[people2(1:2).name] = deal(S{:})
</pre>
<p>If you&#8217;re using release R14 or later, you can omit the DEAL as described in the <a href="http://www.mathworks.com/help/techdoc/rn/f8-1009921.html#f8-1013499" rel="nofollow">Release Notes</a>.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
