Path: news.mathworks.com!newsfeed-00.mathworks.com!NNTP.WPI.EDU!elk.ncren.net!newsflash.concordia.ca!canopus.cc.umanitoba.ca!not-for-mail
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Newsgroups: comp.soft-sys.matlab
Subject: Re: eval begone!
Date: Mon, 21 Apr 2008 04:16:43 +0000 (UTC)
Organization: National Research Council Canada - Conseil national de rechereches Canada
Lines: 28
Message-ID: <fuh4fb$743$1@canopus.cc.umanitoba.ca>
References: <fuh46i$pav$1@fred.mathworks.com>
NNTP-Posting-Host: origin.ibd.nrc.ca
X-Trace: canopus.cc.umanitoba.ca 1208751403 7299 192.70.172.160 (21 Apr 2008 04:16:43 GMT)
X-Complaints-To: abuse@cc.umanitoba.ca
NNTP-Posting-Date: Mon, 21 Apr 2008 04:16:43 +0000 (UTC)
Originator: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Xref: news.mathworks.com comp.soft-sys.matlab:464230


In article <fuh46i$pav$1@fred.mathworks.com>,
jay vaughan <jvaughan5.nospam@gmail.com> wrote:

>I've seen many posts on the Matlab newsgroup stating that
>eval is evil and should be avoided. Upon attempting to
>exorcise my code, I couldn't get rid of one of them...

>Essentially I have a variable 'var' which I want to save
>within a .mat file but with a name other than 'var'. The
>user supplies the name. Here's how I do it now.

>var = [1 2 3];
>answer = inputdlg('choose a name');
>var_name_from_user = answer{1};
>eval([var_name_from_user '=var;']);
>save('saved_file.mat',var_name_from_user);

>Any ideas how to do this without eval? It's probably easy
>but I just can't see it. 


outstruct.(var_name_from_user) = var;
save('saved_file.mat', 'outstruct', '-struct');
-- 
  "Why does he stagger his mind with the mathematics of the sky?
  Once the question mark has arisen in the human brain the answer must
  be found, if it takes a hundred years. A thousand years."
                                              -- Walter Reisch