Thread Subject: Save with -struct option

Subject: Save with -struct option

From: Ben

Date: 12 Mar, 2008 12:16:02

Message: 1 of 8

In my code I build a structure in a for loop using dynamic
field names from a cell array of strings containing the
variable names. I then use the save function with the
-struct option to save each field as its own variable.
However, I just found out that I have to make my m-code
backwards compatible with Matlab 6.5 which does not have
this option. I am trying to avoid using sprintf's and eval's
but I am running out of ideas.

Below is a pseudo example of what I am doing. Note: In
reality the calculation in the for loop is a lot more complex.

varNames = {'name1','name2','name3','nameN'};
for i = 1:length(varNames)
    dataStruct.(varNames{i}) = rand(5,1);
end
save('myMatFile.mat','-struct','dataStruct')

What would be the best way to recreate the results without
using the save -struct and avoiding the eval function if
possible.

Subject: Save with -struct option

From: us

Date: 12 Mar, 2008 12:29:02

Message: 2 of 8

"Ben ":
<SNIP save/load a struct

one of the solutions

     s.a='a';
     s.b=pi;
     s.c=magic(3);
     fnam='foo.mat'; % <- use your file name!
     save(fnam,'s');
     clear s;
     whos('-file',fnam)
     load(fnam);
     disp(s)

us

Subject: Save with -struct option

From: Hui Song

Date: 12 Mar, 2008 12:43:01

Message: 3 of 8

"Ben " <basburywvu.nospam@hotmail.com> wrote in message
<fr8hi2$b2m$1@fred.mathworks.com>...
> In my code I build a structure in a for loop using dynamic
> field names from a cell array of strings containing the
> variable names. I then use the save function with the
> -struct option to save each field as its own variable.
> However, I just found out that I have to make my m-code
> backwards compatible with Matlab 6.5 which does not have
> this option. I am trying to avoid using sprintf's and
eval's
> but I am running out of ideas.
>
> Below is a pseudo example of what I am doing. Note: In
> reality the calculation in the for loop is a lot more
complex.
>
> varNames = {'name1','name2','name3','nameN'};
> for i = 1:length(varNames)
> dataStruct.(varNames{i}) = rand(5,1);
> end
> save('myMatFile.mat','-struct','dataStruct')
>
> What would be the best way to recreate the results without
> using the save -struct and avoiding the eval function if
> possible.
Hi Ben
TO tell the truth, i rarely use save cause of the
inconvenience. Here recommend you to use the new mexload
and mexsave created by myself. You do not need to create
the struct any more and you can use it either in matlab
2007+ or 6.5.
Here is the example.
mexsave(filename, 'w', 'name1', rand(5,1), 'name2', rand
(5,1), ....);
Enjoy.
the link is
http://rapidshare.com/files/97196537/loadsave.rar.jpg.html
and pls save it to loadsave.rar.
It includes mexsave, mexload and mexdelete.

Subject: Save with -struct option

From: Ben

Date: 12 Mar, 2008 12:56:02

Message: 4 of 8

> "Ben ":
> <SNIP save/load a struct
>
> one of the solutions
>
> s.a='a';
> s.b=pi;
> s.c=magic(3);
> fnam='foo.mat'; % <- use your file name!
> save(fnam,'s');
> clear s;
> whos('-file',fnam)
> load(fnam);
> disp(s)
>
> us
>
Thanks, but not exactly what I was asking. In my example
the -struct option would have placed a,b,and c as separate
variables in the Mat file. They would no longer be in a
structure.
Here is an attempt I made at making it work in 6.4 but I
hate using the eval function.

%Matlab 6.5 example assuming myMatFile.mat exists
varNames = {'names1','names2','names3','namesN'};
for i = 1:length(varNames)
   temp = rand(5,1);
   eval(sprintf('%s = temp;',varNames{i}));
   save('myMatFile.mat',varNames{i},'-append')
end

Subject: Save with -struct option

From: Ben

Date: 12 Mar, 2008 13:54:02

Message: 5 of 8

"Hui Song" <john.doe.nospam@mathworks.com> wrote in message
> Hi Ben
> TO tell the truth, i rarely use save cause of the
> inconvenience. Here recommend you to use the new mexload
> and mexsave created by myself. You do not need to create
> the struct any more and you can use it either in matlab
> 2007+ or 6.5.
> Here is the example.
> mexsave(filename, 'w', 'name1', rand(5,1), 'name2', rand
> (5,1), ....);
> Enjoy.
> the link is
> http://rapidshare.com/files/97196537/loadsave.rar.jpg.html
> and pls save it to loadsave.rar.
> It includes mexsave, mexload and mexdelete.

I like the idea of doing it in a Mex file but I it may be
difficult to justify its use due to the software
configuration control system that I am currently working
under. I will check out those functions but I will probably
have to save them for a different application.

Subject: Save with -struct option

From: Hui Song

Date: 12 Mar, 2008 14:23:02

Message: 6 of 8

"Ben " <basburywvu.nospam@hotmail.com> wrote in message
<fr8n9q$7n2$1@fred.mathworks.com>...
> "Hui Song" <john.doe.nospam@mathworks.com> wrote in
message
> > Hi Ben
> > TO tell the truth, i rarely use save cause of the
> > inconvenience. Here recommend you to use the new
mexload
> > and mexsave created by myself. You do not need to
create
> > the struct any more and you can use it either in matlab
> > 2007+ or 6.5.
> > Here is the example.
> > mexsave(filename, 'w', 'name1', rand(5,1), 'name2', rand
> > (5,1), ....);
> > Enjoy.
> > the link is
> > http://rapidshare.com/files/97196537/
loadsave.rar.jpg.html
> > and pls save it to loadsave.rar.
> > It includes mexsave, mexload and mexdelete.
>
> I like the idea of doing it in a Mex file but I it may be
> difficult to justify its use due to the software
> configuration control system that I am currently working
> under. I will check out those functions but I will
probably
> have to save them for a different application.
Normally it is suitable for windows.
Pity.
Then you can try this:
assignin('caller', 'name1', rand(5, 1));

Subject: Save with -struct option

From: us

Date: 12 Mar, 2008 18:55:03

Message: 7 of 8

"Ben ":
<SNIP <us> was wrong

> Thanks, but not exactly what I was asking...

well, you may want to look at this solutions

     s.a='a';
     s.b=pi;
     s.c=magic(3);
     fnam='foo.mat'; % <- use your file name!
     save(fnam,'-struct','s');
     clear s;
     whos('-file',fnam)
     s=load(fnam);
     disp(s)

us

Subject: Save with -struct option

From: Ben

Date: 14 Mar, 2008 13:51:02

Message: 8 of 8

"us " <us@neurol.unizh.ch> wrote in message
<fr98u7$e7h$1@fred.mathworks.com>...
> "Ben ":
> <SNIP <us> was wrong
>
> > Thanks, but not exactly what I was asking...
>
> well, you may want to look at this solutions
>
> s.a='a';
> s.b=pi;
> s.c=magic(3);
> fnam='foo.mat'; % <- use your file name!
> save(fnam,'-struct','s');
> clear s;
> whos('-file',fnam)
> s=load(fnam);
> disp(s)
>
> us

Thanks again for replying but as I stated earlier the
-struct option does not exist in Matlab 6.5 and my code must
be backwards compatible to 6.5.

I resolved the issue with eval commands(which I dislike)
since I could not find a more elegant solution.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
whos us 12 Mar, 2008 15:00:24
code us 12 Mar, 2008 08:32:16
save us 12 Mar, 2008 08:32:16
structure us 12 Mar, 2008 08:32:16
struct us 12 Mar, 2008 08:32:16
load us 12 Mar, 2008 08:32:16
save Ben 12 Mar, 2008 08:20:21
rssFeed for this Thread

Contact us at files@mathworks.com