<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165516</link>
    <title>MATLAB Central Newsreader - Save with -struct option</title>
    <description>Feed for thread: Save with -struct option</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>Wed, 12 Mar 2008 12:16:02 -0400</pubDate>
      <title>Save with -struct option</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165516#420432</link>
      <author>Ben </author>
      <description>In my code I build a structure in a for loop using dynamic&lt;br&gt;
field names from a cell array of strings containing the&lt;br&gt;
variable names.  I then use the save function with the&lt;br&gt;
-struct option to save each field as its own variable. &lt;br&gt;
However, I just found out that I have to make my m-code&lt;br&gt;
backwards compatible with Matlab 6.5 which does not have&lt;br&gt;
this option. I am trying to avoid using sprintf's and eval's&lt;br&gt;
but I am running out of ideas.  &lt;br&gt;
&lt;br&gt;
Below is a pseudo example of what I am doing.  Note: In&lt;br&gt;
reality the calculation in the for loop is a lot more complex.&lt;br&gt;
&lt;br&gt;
varNames = {'name1','name2','name3','nameN'};&lt;br&gt;
for i = 1:length(varNames)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dataStruct.(varNames{i}) = rand(5,1);&lt;br&gt;
end&lt;br&gt;
save('myMatFile.mat','-struct','dataStruct')  &lt;br&gt;
&lt;br&gt;
What would be the best way to recreate the results without&lt;br&gt;
using the save -struct and avoiding the eval function if&lt;br&gt;
possible. </description>
    </item>
    <item>
      <pubDate>Wed, 12 Mar 2008 12:29:02 -0400</pubDate>
      <title>Re: Save with -struct option</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165516#420434</link>
      <author>us</author>
      <description>&quot;Ben &quot;:&lt;br&gt;
&amp;lt;SNIP save/load a struct&lt;br&gt;
&lt;br&gt;
one of the solutions&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s.a='a';&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s.b=pi;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s.c=magic(3);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fnam='foo.mat'; % &amp;lt;- use your file name!&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;save(fnam,'s');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;clear s;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;whos('-file',fnam)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;load(fnam);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp(s)&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
    <item>
      <pubDate>Wed, 12 Mar 2008 12:43:01 -0400</pubDate>
      <title>Re: Save with -struct option</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165516#420438</link>
      <author>Hui Song</author>
      <description>&quot;Ben &quot; &amp;lt;basburywvu.nospam@hotmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;fr8hi2$b2m$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; In my code I build a structure in a for loop using dynamic&lt;br&gt;
&amp;gt; field names from a cell array of strings containing the&lt;br&gt;
&amp;gt; variable names.  I then use the save function with the&lt;br&gt;
&amp;gt; -struct option to save each field as its own variable. &lt;br&gt;
&amp;gt; However, I just found out that I have to make my m-code&lt;br&gt;
&amp;gt; backwards compatible with Matlab 6.5 which does not have&lt;br&gt;
&amp;gt; this option. I am trying to avoid using sprintf's and &lt;br&gt;
eval's&lt;br&gt;
&amp;gt; but I am running out of ideas.  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Below is a pseudo example of what I am doing.  Note: In&lt;br&gt;
&amp;gt; reality the calculation in the for loop is a lot more &lt;br&gt;
complex.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; varNames = {'name1','name2','name3','nameN'};&lt;br&gt;
&amp;gt; for i = 1:length(varNames)&lt;br&gt;
&amp;gt;     dataStruct.(varNames{i}) = rand(5,1);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; save('myMatFile.mat','-struct','dataStruct')  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; What would be the best way to recreate the results without&lt;br&gt;
&amp;gt; using the save -struct and avoiding the eval function if&lt;br&gt;
&amp;gt; possible. &lt;br&gt;
Hi Ben&lt;br&gt;
TO tell the truth, i rarely use save cause of the &lt;br&gt;
inconvenience. Here recommend you to use the new mexload &lt;br&gt;
and mexsave created by myself. You do not need to create &lt;br&gt;
the struct any more and you can use it either in matlab &lt;br&gt;
2007+ or 6.5.&lt;br&gt;
Here is the example.&lt;br&gt;
mexsave(filename, 'w', 'name1', rand(5,1), 'name2', rand&lt;br&gt;
(5,1), ....);&lt;br&gt;
Enjoy.&lt;br&gt;
the link is&lt;br&gt;
&lt;a href=&quot;http://rapidshare.com/files/97196537/loadsave.rar.jpg.html&quot;&gt;http://rapidshare.com/files/97196537/loadsave.rar.jpg.html&lt;/a&gt;&lt;br&gt;
and pls save it to loadsave.rar.&lt;br&gt;
It includes mexsave, mexload and mexdelete.</description>
    </item>
    <item>
      <pubDate>Wed, 12 Mar 2008 12:56:02 -0400</pubDate>
      <title>Re: Save with -struct option</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165516#420444</link>
      <author>Ben </author>
      <description>&amp;gt; &quot;Ben &quot;:&lt;br&gt;
&amp;gt; &amp;lt;SNIP save/load a struct&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; one of the solutions&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      s.a='a';&lt;br&gt;
&amp;gt;      s.b=pi;&lt;br&gt;
&amp;gt;      s.c=magic(3);&lt;br&gt;
&amp;gt;      fnam='foo.mat'; % &amp;lt;- use your file name!&lt;br&gt;
&amp;gt;      save(fnam,'s');&lt;br&gt;
&amp;gt;      clear s;&lt;br&gt;
&amp;gt;      whos('-file',fnam)&lt;br&gt;
&amp;gt;      load(fnam);&lt;br&gt;
&amp;gt;      disp(s)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; us&lt;br&gt;
&amp;gt; &lt;br&gt;
Thanks, but not exactly what I was asking.  In my example&lt;br&gt;
the -struct option would have placed a,b,and c as separate&lt;br&gt;
variables in the Mat file.  They would no longer be in a&lt;br&gt;
structure. &lt;br&gt;
Here is an attempt I made at making it work in 6.4 but I&lt;br&gt;
hate using the eval function.&lt;br&gt;
&lt;br&gt;
%Matlab 6.5 example assuming myMatFile.mat exists&lt;br&gt;
varNames = {'names1','names2','names3','namesN'};&lt;br&gt;
for i = 1:length(varNames)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;temp = rand(5,1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;eval(sprintf('%s = temp;',varNames{i}));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;save('myMatFile.mat',varNames{i},'-append')&lt;br&gt;
end</description>
    </item>
    <item>
      <pubDate>Wed, 12 Mar 2008 13:54:02 -0400</pubDate>
      <title>Re: Save with -struct option</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165516#420454</link>
      <author>Ben </author>
      <description>&quot;Hui Song&quot; &amp;lt;john.doe.nospam@mathworks.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; Hi Ben&lt;br&gt;
&amp;gt; TO tell the truth, i rarely use save cause of the &lt;br&gt;
&amp;gt; inconvenience. Here recommend you to use the new mexload &lt;br&gt;
&amp;gt; and mexsave created by myself. You do not need to create &lt;br&gt;
&amp;gt; the struct any more and you can use it either in matlab &lt;br&gt;
&amp;gt; 2007+ or 6.5.&lt;br&gt;
&amp;gt; Here is the example.&lt;br&gt;
&amp;gt; mexsave(filename, 'w', 'name1', rand(5,1), 'name2', rand&lt;br&gt;
&amp;gt; (5,1), ....);&lt;br&gt;
&amp;gt; Enjoy.&lt;br&gt;
&amp;gt; the link is&lt;br&gt;
&amp;gt; &lt;a href=&quot;http://rapidshare.com/files/97196537/loadsave.rar.jpg.html&quot;&gt;http://rapidshare.com/files/97196537/loadsave.rar.jpg.html&lt;/a&gt;&lt;br&gt;
&amp;gt; and pls save it to loadsave.rar.&lt;br&gt;
&amp;gt; It includes mexsave, mexload and mexdelete.&lt;br&gt;
&lt;br&gt;
I like the idea of doing it in a Mex file but I it may be&lt;br&gt;
difficult to justify its use due to the software&lt;br&gt;
configuration control system that I am currently working&lt;br&gt;
under.  I will check out those functions but I will probably&lt;br&gt;
have to save them for a different application.</description>
    </item>
    <item>
      <pubDate>Wed, 12 Mar 2008 14:23:02 -0400</pubDate>
      <title>Re: Save with -struct option</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165516#420462</link>
      <author>Hui Song</author>
      <description>&quot;Ben &quot; &amp;lt;basburywvu.nospam@hotmail.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;fr8n9q$7n2$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Hui Song&quot; &amp;lt;john.doe.nospam@mathworks.com&amp;gt; wrote in &lt;br&gt;
message &lt;br&gt;
&amp;gt; &amp;gt; Hi Ben&lt;br&gt;
&amp;gt; &amp;gt; TO tell the truth, i rarely use save cause of the &lt;br&gt;
&amp;gt; &amp;gt; inconvenience. Here recommend you to use the new &lt;br&gt;
mexload &lt;br&gt;
&amp;gt; &amp;gt; and mexsave created by myself. You do not need to &lt;br&gt;
create &lt;br&gt;
&amp;gt; &amp;gt; the struct any more and you can use it either in matlab &lt;br&gt;
&amp;gt; &amp;gt; 2007+ or 6.5.&lt;br&gt;
&amp;gt; &amp;gt; Here is the example.&lt;br&gt;
&amp;gt; &amp;gt; mexsave(filename, 'w', 'name1', rand(5,1), 'name2', rand&lt;br&gt;
&amp;gt; &amp;gt; (5,1), ....);&lt;br&gt;
&amp;gt; &amp;gt; Enjoy.&lt;br&gt;
&amp;gt; &amp;gt; the link is&lt;br&gt;
&amp;gt; &amp;gt; &lt;a href=&quot;http://rapidshare.com/files/97196537/&quot;&gt;http://rapidshare.com/files/97196537/&lt;/a&gt;&lt;br&gt;
loadsave.rar.jpg.html&lt;br&gt;
&amp;gt; &amp;gt; and pls save it to loadsave.rar.&lt;br&gt;
&amp;gt; &amp;gt; It includes mexsave, mexload and mexdelete.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I like the idea of doing it in a Mex file but I it may be&lt;br&gt;
&amp;gt; difficult to justify its use due to the software&lt;br&gt;
&amp;gt; configuration control system that I am currently working&lt;br&gt;
&amp;gt; under.  I will check out those functions but I will &lt;br&gt;
probably&lt;br&gt;
&amp;gt; have to save them for a different application.&lt;br&gt;
Normally it is suitable for windows.&lt;br&gt;
Pity.&lt;br&gt;
Then you can try this:&lt;br&gt;
assignin('caller', 'name1', rand(5, 1));</description>
    </item>
    <item>
      <pubDate>Wed, 12 Mar 2008 18:55:03 -0400</pubDate>
      <title>Re: Save with -struct option</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165516#420542</link>
      <author>us</author>
      <description>&quot;Ben &quot;:&lt;br&gt;
&amp;lt;SNIP &amp;lt;us&amp;gt; was wrong&lt;br&gt;
&lt;br&gt;
&amp;gt; Thanks, but not exactly what I was asking...&lt;br&gt;
&lt;br&gt;
well, you may want to look at this solutions&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s.a='a';&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s.b=pi;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s.c=magic(3);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fnam='foo.mat'; % &amp;lt;- use your file name!&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;save(fnam,'-struct','s');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;clear s;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;whos('-file',fnam)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s=load(fnam);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp(s)&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
    <item>
      <pubDate>Fri, 14 Mar 2008 13:51:02 -0400</pubDate>
      <title>Re: Save with -struct option</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/165516#420903</link>
      <author>Ben </author>
      <description>&quot;us &quot; &amp;lt;us@neurol.unizh.ch&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fr98u7$e7h$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Ben &quot;:&lt;br&gt;
&amp;gt; &amp;lt;SNIP &amp;lt;us&amp;gt; was wrong&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Thanks, but not exactly what I was asking...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; well, you may want to look at this solutions&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      s.a='a';&lt;br&gt;
&amp;gt;      s.b=pi;&lt;br&gt;
&amp;gt;      s.c=magic(3);&lt;br&gt;
&amp;gt;      fnam='foo.mat'; % &amp;lt;- use your file name!&lt;br&gt;
&amp;gt;      save(fnam,'-struct','s');&lt;br&gt;
&amp;gt;      clear s;&lt;br&gt;
&amp;gt;      whos('-file',fnam)&lt;br&gt;
&amp;gt;      s=load(fnam);&lt;br&gt;
&amp;gt;      disp(s)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; us&lt;br&gt;
&lt;br&gt;
Thanks again for replying but as I stated earlier the&lt;br&gt;
-struct option does not exist in Matlab 6.5 and my code must&lt;br&gt;
be backwards compatible to 6.5.&lt;br&gt;
&lt;br&gt;
I resolved the issue with eval commands(which I dislike)&lt;br&gt;
since I could not find a more elegant solution.</description>
    </item>
  </channel>
</rss>

