Re: eval begone!



In article <fuh46i$pav$1@xxxxxxxxxxxxxxxxxx>,
jay vaughan <jvaughan5.nospam@xxxxxxxxx> 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
.



Relevant Pages

  • eval begone!
    ... eval is evil and should be avoided. ... exorcise my code, I couldn't get rid of one of them... ... Essentially I have a variable 'var' which I want to save ...
    (comp.soft-sys.matlab)
  • Re: Death Note - ep 1
    ... Brilliant high school kid gains the power of death over just ... Decides remaking the world to get rid of ... evil people is the way to go. ...
    (rec.arts.anime.misc)
  • Re: A storm
    ... I see we're both using insectoid similes to describe the troles. ... you want to get rid of the smell:-) ... You try to drive people to suicide - that's evil. ... My behaviour is perfectly okay; ...
    (uk.people.support.depression)
  • Re: OT: not sure this can be discussed on a public forum.... toes
    ... evil stuff - made me ill as hell for six months. ... Fungi are utter bastards to get rid of, ...
    (uk.rec.motorcycles)
  • Re: Stan Lee, you tard!
    ... And what was the deal with him calling Stan "Boss"? ... instead of an evil mastermind? ... And they got rid of Monkey Woman. ... Because she's an actress wannabe... ...
    (rec.arts.sf.tv)