Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: eval begone!

Subject: eval begone!

From: jay vaughan

Date: 21 Apr, 2008 04:12:02

Message: 1 of 3

Hi,

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.

Thanks,
J

Subject: Re: eval begone!

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 21 Apr, 2008 04:16:43

Message: 2 of 3

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

Subject: Re: eval begone!

From: jay vaughan

Date: 21 Apr, 2008 04:45:03

Message: 3 of 3


Walter,

Very clever...I like it.


Thanks,

J


P.S. Minor detail...I had to put '-struct' before struct
name 'outstruct' as shown below...

save('saved_file.mat', '-struct', 'outstruct');


roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fuh4fb$743$1@canopus.cc.umanitoba.ca>...
> 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

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
eval jay vaughan 21 Apr, 2008 00:15:04
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics