Path: news.mathworks.com!not-for-mail
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$1@fred.mathworks.com>
References: <f7olca$1mj$1@fred.mathworks.com> <f7om7m$dvb$1@fred.mathworks.com> <gi9c71$c2a$1@fred.mathworks.com> <WlX1l.5935$%z5.391@newsfe09.iad> <gib8ib$p3p$1@fred.mathworks.com> <gibebb$ft8$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1229537420 19748 172.30.248.38 (17 Dec 2008 18:10:20 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 17 Dec 2008 18:10:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com 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?