Path: news.mathworks.com!not-for-mail
From: "Ben " <basburywvu.nospam@hotmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Save with -struct option
Date: Wed, 12 Mar 2008 12:16:02 +0000 (UTC)
Organization: Orbital Sciences Corp
Lines: 21
Message-ID: <fr8hi2$b2m$1@fred.mathworks.com>
Reply-To: "Ben " <basburywvu.nospam@hotmail.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1205324162 11350 172.30.248.37 (12 Mar 2008 12:16:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 12 Mar 2008 12:16:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 425457
Xref: news.mathworks.com comp.soft-sys.matlab:456769



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.