<?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: Structure Initialization</title>
	<atom:link href="http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Loren Shure</title>
		<link>http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32828</link>
		<dc:creator>Loren Shure</dc:creator>
		<pubDate>Fri, 09 Dec 2011 15:13:06 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32828</guid>
		<description>Gerard-

Please contact technical support for more help.  I don&#039;t exactly understand your situation and am not near MATLAB to try things out.

--Loren</description>
		<content:encoded><![CDATA[<p>Gerard-</p>
<p>Please contact technical support for more help.  I don&#8217;t exactly understand your situation and am not near MATLAB to try things out.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gerard</title>
		<link>http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32825</link>
		<dc:creator>Gerard</dc:creator>
		<pubDate>Fri, 09 Dec 2011 10:34:16 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32825</guid>
		<description>Hi Loren, thanks for your support again.

In my case, each &#039;fieldName&#039; contains a vector, so &#039;myStruct(:).fieldName&#039; does only produce the first &#039;fieldName&#039; vector, the same as (myStruct(:).fieldName).

Only {myStruct(:).fieldName} is left. Could you help me out on how to concatenate this in a for loop? This doesn&#039;t seem correct:

fieldNames = {&#039;field1&#039;, ... , &#039;fieldN&#039;}

for C=fieldNames
    temp = { myStruct(:).(C{1}) };

    for i=1:size(myStruct.(C{1}),2)
        dataCat.(C{1}) = [dataCat.(C{1}) temp{i}];
    end

end</description>
		<content:encoded><![CDATA[<p>Hi Loren, thanks for your support again.</p>
<p>In my case, each &#8216;fieldName&#8217; contains a vector, so &#8216;myStruct(:).fieldName&#8217; does only produce the first &#8216;fieldName&#8217; vector, the same as (myStruct(:).fieldName).</p>
<p>Only {myStruct(:).fieldName} is left. Could you help me out on how to concatenate this in a for loop? This doesn&#8217;t seem correct:</p>
<p>fieldNames = {&#8216;field1&#8242;, &#8230; , &#8216;fieldN&#8217;}</p>
<p>for C=fieldNames<br />
    temp = { myStruct(:).(C{1}) };</p>
<p>    for i=1:size(myStruct.(C{1}),2)<br />
        dataCat.(C{1}) = [dataCat.(C{1}) temp{i}];<br />
    end</p>
<p>end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren Shure</title>
		<link>http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32820</link>
		<dc:creator>Loren Shure</dc:creator>
		<pubDate>Thu, 08 Dec 2011 17:59:19 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32820</guid>
		<description>Gerard-

myStruct(:).fieldname produces a comma-separated list in MATLAB (see the doc to learn more about this). To collect all the outputs, assuming they are scalar numeric, into a single vector, enclose the expression in square brackets [myStruct(:).fieldname].  To stick the values into a cell array, in case they won&#039;t concatenate properly, do the same trick but wrap with curly braces {myStruct(:).fieldname}

--Loren</description>
		<content:encoded><![CDATA[<p>Gerard-</p>
<p>myStruct(:).fieldname produces a comma-separated list in MATLAB (see the doc to learn more about this). To collect all the outputs, assuming they are scalar numeric, into a single vector, enclose the expression in square brackets [myStruct(:).fieldname].  To stick the values into a cell array, in case they won&#8217;t concatenate properly, do the same trick but wrap with curly braces {myStruct(:).fieldname}</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gerard</title>
		<link>http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32819</link>
		<dc:creator>Gerard</dc:creator>
		<pubDate>Thu, 08 Dec 2011 17:14:52 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32819</guid>
		<description>Thanks Peter. Well then, I guess I will have to stick on my code, since datasets cannot handle size and multi-D. It is a pity, they look very flexible and efficient for vectorizing and also for code simplification.

I won&#039;t get rid of the two for loops that I have to carry during the whole code :( (I have to make calculations for each run and for every field).

One more question: how to retrieve data from a field for all the structures of an array of structures? That is, this line

myStruct(:).fieldName

does only retrieve myStruct(1).fieldName, but not all of them.</description>
		<content:encoded><![CDATA[<p>Thanks Peter. Well then, I guess I will have to stick on my code, since datasets cannot handle size and multi-D. It is a pity, they look very flexible and efficient for vectorizing and also for code simplification.</p>
<p>I won&#8217;t get rid of the two for loops that I have to carry during the whole code :( (I have to make calculations for each run and for every field).</p>
<p>One more question: how to retrieve data from a field for all the structures of an array of structures? That is, this line</p>
<p>myStruct(:).fieldName</p>
<p>does only retrieve myStruct(1).fieldName, but not all of them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Perkins</title>
		<link>http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32805</link>
		<dc:creator>Peter Perkins</dc:creator>
		<pubDate>Tue, 06 Dec 2011 19:12:22 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32805</guid>
		<description>Gerard, two things:

&gt; This means I still have not created any variables of the
&gt; bin files. I am just reading the files DIRECTLY into
&gt; each field of the structure.

Sure you have. You just haven’t given them a name. They’re temporary values that exist only for the lifespan of that one line. You can do the same thing with dataset.

&gt; Also, the bin files have different data size. ‘dataset’
&gt; is apparently used with equal size variables.

Yes. A dataset array is like a table where each column (dataset variable) has the same length. So if your data aren’t like that, then dataset isn’t for you.  The 2-D-ness is perhaps also a problem, but dataset arrays do allow you to have variables that are themselves matrices.  That can sometimes be convenient for higher dimensional data.</description>
		<content:encoded><![CDATA[<p>Gerard, two things:</p>
<p>&gt; This means I still have not created any variables of the<br />
&gt; bin files. I am just reading the files DIRECTLY into<br />
&gt; each field of the structure.</p>
<p>Sure you have. You just haven’t given them a name. They’re temporary values that exist only for the lifespan of that one line. You can do the same thing with dataset.</p>
<p>&gt; Also, the bin files have different data size. ‘dataset’<br />
&gt; is apparently used with equal size variables.</p>
<p>Yes. A dataset array is like a table where each column (dataset variable) has the same length. So if your data aren’t like that, then dataset isn’t for you.  The 2-D-ness is perhaps also a problem, but dataset arrays do allow you to have variables that are themselves matrices.  That can sometimes be convenient for higher dimensional data.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gerard</title>
		<link>http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32802</link>
		<dc:creator>Gerard</dc:creator>
		<pubDate>Tue, 06 Dec 2011 16:22:23 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32802</guid>
		<description>Thanks for your support Peter. 

In my case, the &#039;fieldNames&#039; cell array contains the strings that are, at the same time, both the names of each bin file to be imported AND the field names I want to use as variables in the structures.

This means I still have not created any variables of the bin files. I am just reading the files DIRECTLY into each field of the structure.


mystruct(i).(c{1}) = fread( [c{1} &#039;.bin&#039;] ); % It was maybe unclear before, but I read a whole vector in each field of the structure, not a single data value.
% Each &#039;i&#039; has the run number and each field has a vector of data.


I don&#039;t see how could I read these files DIRECTLY into a dataset (seems it is not possible, as you say). So I should first read the files into variables anyway using my code, and then create the dataset. This looks a bit messy to me.

Also, the bin files have different data size. &#039;dataset&#039; is apparently used with equal size variables. When used with different variable sizes I cannot visualize it well in the workspace (harder to debug, then?). Moreover, with my code I have now an array of structures that contains a different structure for each run, all packed in one variable in the workspace (very clean). I guess dataset arrays cannot have more than two dimensions (?), so as to make the third dimension the run number (this way I could have sheets of datasets for each run, like excel sheets, and access them very easily).

Are in this case dataset arrays still worth the effort?</description>
		<content:encoded><![CDATA[<p>Thanks for your support Peter. </p>
<p>In my case, the &#8216;fieldNames&#8217; cell array contains the strings that are, at the same time, both the names of each bin file to be imported AND the field names I want to use as variables in the structures.</p>
<p>This means I still have not created any variables of the bin files. I am just reading the files DIRECTLY into each field of the structure.</p>
<p>mystruct(i).(c{1}) = fread( [c{1} '.bin'] ); % It was maybe unclear before, but I read a whole vector in each field of the structure, not a single data value.<br />
% Each &#8216;i&#8217; has the run number and each field has a vector of data.</p>
<p>I don&#8217;t see how could I read these files DIRECTLY into a dataset (seems it is not possible, as you say). So I should first read the files into variables anyway using my code, and then create the dataset. This looks a bit messy to me.</p>
<p>Also, the bin files have different data size. &#8216;dataset&#8217; is apparently used with equal size variables. When used with different variable sizes I cannot visualize it well in the workspace (harder to debug, then?). Moreover, with my code I have now an array of structures that contains a different structure for each run, all packed in one variable in the workspace (very clean). I guess dataset arrays cannot have more than two dimensions (?), so as to make the third dimension the run number (this way I could have sheets of datasets for each run, like excel sheets, and access them very easily).</p>
<p>Are in this case dataset arrays still worth the effort?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Perkins</title>
		<link>http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32798</link>
		<dc:creator>Peter Perkins</dc:creator>
		<pubDate>Tue, 06 Dec 2011 12:00:42 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32798</guid>
		<description>Gerard, there are two ways to create a dataset array from scratch: from workspace variables, or by reading from a text or Excel file.  In you case, if possible, it would be fastest to create one dataset variable at a time by reversing the order of your loops, and putting something like this line inside the outer (fields) loop, but following the inner (runs) loop:

myData.(varNames{j}) = aVectorOfData;

You can certainly use your existing code, and replace the innermost line with

myData.(c{1})(i) = someData;

but it is fastest to work on one entire variable at a time.

In either case, you would need to preallocate the dataset array before doing the above, much as you did for the structure array.</description>
		<content:encoded><![CDATA[<p>Gerard, there are two ways to create a dataset array from scratch: from workspace variables, or by reading from a text or Excel file.  In you case, if possible, it would be fastest to create one dataset variable at a time by reversing the order of your loops, and putting something like this line inside the outer (fields) loop, but following the inner (runs) loop:</p>
<p>myData.(varNames{j}) = aVectorOfData;</p>
<p>You can certainly use your existing code, and replace the innermost line with</p>
<p>myData.(c{1})(i) = someData;</p>
<p>but it is fastest to work on one entire variable at a time.</p>
<p>In either case, you would need to preallocate the dataset array before doing the above, much as you did for the structure array.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gerard</title>
		<link>http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32795</link>
		<dc:creator>Gerard</dc:creator>
		<pubDate>Mon, 05 Dec 2011 18:54:06 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32795</guid>
		<description>Thank you Loren, I have been checking the statistics toolbox and looks very promising.

However, in all examples they generate datasets from already existing variables. I do not see how could you open, say, 10 bin files and store them directly into a dataset with the corresponding label from the cell array (which is what I am doing in my code if I replace &#039;someData&#039; by e.g. &#039;fread&#039; function).</description>
		<content:encoded><![CDATA[<p>Thank you Loren, I have been checking the statistics toolbox and looks very promising.</p>
<p>However, in all examples they generate datasets from already existing variables. I do not see how could you open, say, 10 bin files and store them directly into a dataset with the corresponding label from the cell array (which is what I am doing in my code if I replace &#8216;someData&#8217; by e.g. &#8216;fread&#8217; function).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren Shure</title>
		<link>http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32794</link>
		<dc:creator>Loren Shure</dc:creator>
		<pubDate>Mon, 05 Dec 2011 15:16:54 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32794</guid>
		<description>Gerard-

If you have access to Statistics Toolbox, you may find dataset arrays very useful for your application.  The idea of arrays of structures can be quite useful, but it is costly in terms of memory and access.

--Loren</description>
		<content:encoded><![CDATA[<p>Gerard-</p>
<p>If you have access to Statistics Toolbox, you may find dataset arrays very useful for your application.  The idea of arrays of structures can be quite useful, but it is costly in terms of memory and access.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gerard</title>
		<link>http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32793</link>
		<dc:creator>Gerard</dc:creator>
		<pubDate>Mon, 05 Dec 2011 15:07:46 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/02/01/structure-initialization/#comment-32793</guid>
		<description>My contribution.

In my lab we make many runs of our experiment. Each run is a data set consisting of many fields (time, energy, polarization, momentum, jitter...). This means that I need to create an array of structures. Each structure contains the data for a run with these field names.

The problem is that each structure contains maaaany fields, and I do NOT want to write each field separately as you do here. Instead, I first create a cell array with the strings of every field name, like this:

fieldNames = {&#039;field1&#039;, &#039;field2&#039;, ... , &#039;fieldN&#039;};


Now I will pass each of this string variables in the cell as a dynamic fieldname to my structure, that is: 

for c = fieldNames
mystruct.(c{1}) = someData; % In each iteration c is a 1x1 cell array containing the next fieldName.
end


Now, this should work for a single structure with maaany fields. In my case I still need more than this. I need an array of such structures, one for each run of experiment. In that case, I will include another for loop that goes through each run number. I will also need to initialize somehow this array of structures. Here&#039;s the whole (but simple) code:

fieldNames = {&#039;field1&#039;, &#039;field2&#039;, ... , &#039;fieldN&#039;};
struct(numberOfRuns).field1 = 0; % Initialize the array of structures with at least one field each.
for i=1:numberOfRuns
for c = fieldNames
mystruct(i).(c{1}) = someData;
end
end


In my opinion this is an elegant (at least code-friendly) way to work with large arrays of structures that contain many fields. A good analogy is a dataBase containing many other personal data besides the usual &#039;name&#039; and &#039;height&#039;.

I would anyway appreciate comments on the efficiency of this code.

Thanks!</description>
		<content:encoded><![CDATA[<p>My contribution.</p>
<p>In my lab we make many runs of our experiment. Each run is a data set consisting of many fields (time, energy, polarization, momentum, jitter&#8230;). This means that I need to create an array of structures. Each structure contains the data for a run with these field names.</p>
<p>The problem is that each structure contains maaaany fields, and I do NOT want to write each field separately as you do here. Instead, I first create a cell array with the strings of every field name, like this:</p>
<p>fieldNames = {&#8216;field1&#8242;, &#8216;field2&#8242;, &#8230; , &#8216;fieldN&#8217;};</p>
<p>Now I will pass each of this string variables in the cell as a dynamic fieldname to my structure, that is: </p>
<p>for c = fieldNames<br />
mystruct.(c{1}) = someData; % In each iteration c is a 1&#215;1 cell array containing the next fieldName.<br />
end</p>
<p>Now, this should work for a single structure with maaany fields. In my case I still need more than this. I need an array of such structures, one for each run of experiment. In that case, I will include another for loop that goes through each run number. I will also need to initialize somehow this array of structures. Here&#8217;s the whole (but simple) code:</p>
<p>fieldNames = {&#8216;field1&#8242;, &#8216;field2&#8242;, &#8230; , &#8216;fieldN&#8217;};<br />
struct(numberOfRuns).field1 = 0; % Initialize the array of structures with at least one field each.<br />
for i=1:numberOfRuns<br />
for c = fieldNames<br />
mystruct(i).(c{1}) = someData;<br />
end<br />
end</p>
<p>In my opinion this is an elegant (at least code-friendly) way to work with large arrays of structures that contain many fields. A good analogy is a dataBase containing many other personal data besides the usual &#8216;name&#8217; and &#8216;height&#8217;.</p>
<p>I would anyway appreciate comments on the efficiency of this code.</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

