From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to rename a workspace variable in a loop?
Date: Wed, 17 Dec 2008 18:10:20 +0000 (UTC)
Organization: Xoran Technologies
Lines: 21
Message-ID: <gibfac$j94$>
References: <f7olca$1mj$> <f7om7m$dvb$> <gi9c71$c2a$> <WlX1l.5935$%z5.391@newsfe09.iad> <gib8ib$p3p$> <gibebb$ft8$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1229537420 19748 (17 Dec 2008 18:10:20 GMT)
NNTP-Posting-Date: Wed, 17 Dec 2008 18:10:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: comp.soft-sys.matlab:507578

> for k = 1:size(X, 3)
>     S.(sprintf('slice%d', k)) = X(:, :, k);
> end
> save(OutputFileName,'S', '-struct');
> Unless your X has a lot of pages, though, the FOR 
> loop won't have to execute for very long.  And IMO it's more easily read 
> than Walter's two-liner.

It has about 600 pages, but it doesn't matter. The actual execution of the save() command will clearly be the bottleneck here, regardless of the number of pages.

> The use of an array is insufficient in this case.  The use of a combination 
> of dynamic field names and the -struct flag for SAVE is sufficient to 
> accomplish the goal as you stated:  "later you want to load in one slice at 
> a time for some sort of slice-wise processing."

I don't draw much of a distinction between dynamic field names and dynamic variable names. They're about the same in their internal implementation anyway, no?