Executing eval in function handle ?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have the following code which does not work and I don't understand why:
foo='name'
name='C23';
handle=@(var) eval(var)
handle(foo)
I get the following error:
Error using eval
Undefined function or variable 'name'.
Error in @(var)eval(var)
Here,
handle(foo)
should return
'C23'
Can someone tell me what I did wrong and how to make this work?
Thanks in advance
3 commentaires
Stephen23
le 19 Mar 2018
"I could not think of any other way to do this"
Using eval is rarely the solution that beginners think it is: it will just make your code slow, complex, and (as you are finding out now) much buggier and harder to debug. Read this to know more:
Réponse acceptée
Stephen23
le 19 Mar 2018
Modifié(e) : Stephen23
le 19 Mar 2018
This is a classic example of how the decision to use eval to access variables just makes code more complex and introduces more problems than it solves. The name MATLAB comes from "MATrix LABoratory": when you put your data into matrices/arrays then this problem is easy to solve. Thus the first step is to simply put the input data into one cell array (which they should be in anyway, rather then in separate variables):
>> nameOrder = {'sur','given'};
>> sname = 'Smith';
>> gname = 'Jane';
>> tmp = {gname,sname}; % first step: put into one array!
>> [~,idx] = ismember(nameOrder,{'given','sur'});
>> sprintf('%s_%s',tmp{idx})
ans = Smith_Jane
Simple, reasonably efficient, and it avoids all of the pointless problems of eval. I changed the variable names and char vectors to make it clear that the nameOrder contents are not the same as the variable names. You could simplify it even more by storing the names in a standard format, e.g. perhaps a structure:
S.surname = 'Smith';
S.givenname = 'Jane';
tmp = {S.givenname,S.surname};
Note that using a cell array has the advantage that you could write code which takes into account cultures which have multiple or only one name, all of which could be handled trivially with indexing (and not an eval in sight!).
Summary: keep data together as much as possible, rather than splitting it apart. Keeping data together makes it easier to work with.
2 commentaires
Plus de réponses (1)
Greg
le 19 Mar 2018
Most importantly! respond to Rik's comment above. The use of eval is very nearly always a horrible idea.
However, to your question: it does not work because the scope of name is where the anonymous function is defined. When executing the anonymous function, name is out of scope in that workspace.
1 commentaire
Greg
le 19 Mar 2018
I hesitate to mention it could work if you used evalin('caller',...); but again, bad idea!
Voir également
Catégories
En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!