What happened to eval function?

I am using R2022a, and the eval function is giving me an error,
eval(['4 + 5'])
ans = 9
Unrecognized function or variable 'eval'.
Ignore "ans = 9" in my question, since that's what happened when I ran it online in R2022a; that is the expected behavior but not what I'm getting on my computer.
Any idea what's going on? I tried restarting Matlab and the error persisted.

2 comentarios

dpb
dpb el 15 de Jun. de 2022
That's a munged install or somesuch...what doe
which -all eval
return?
eval is to be avoided unless absolutely necessary (which rarely is) but it still exists and something is amiss in the installation or path if it isn't found.
KAE
KAE el 17 de Jun. de 2022
>> which -all eval
built-in (C:\Program Files\MATLAB\R2022a\toolbox\matlab\lang\eval)
C:\Program Files\MATLAB\R2022a\toolbox\matlab\lang\@opaque\eval.m % opaque method
C:\Users\MyName\OneDrive - datacolor\matlab_programs\data processing\fdaM\@bifd\eval.m % bifd method

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 16 de Jun. de 2022

1 voto

I think maybe you could get this error under the odd conditions:
  • you are inside a function
  • you assign to a variable named eval
  • you run a script
  • the script clears the variable named eval
  • after the script you try to use eval
I am not positive this will generate that exact error, but MATLAB is allowed to lose track of functions and variables if you do this or you assign to a name inside the script when the name matches a function name

3 comentarios

KAE
KAE el 17 de Jun. de 2022
Editada: KAE el 17 de Jun. de 2022
I am sure a flavor of this is what happened (yes, inside a function; no, did not assign a variable named eval). One variable was set equal to a second variable, and the second's name was given by an eval statement. When I pulled the whole assignment inside eval, the problem went away. Yet I can't create a one-line example that does this, so the fact it occurred inside a function could indeed have been the problem.
I was worried that R2022a had eliminated eval, which for me is a necessary evil (just change the "i" to an "a", ha ha). I avoid eval, but for some data processing code I find it useful.
Image Analyst
Image Analyst el 17 de Jun. de 2022
Editada: Image Analyst el 17 de Jun. de 2022
I can't imagine how it's necessary for you. Maybe if you explained the use case. It's say it's an unnecessary evil. I don't believe you EVER need to create a variable name from some string or other kind of expression. So you shouldn't need to use the evil eval.
Again, what happened when you did what @dpb said:
>> which -all eval
I see
built-in (C:\Program Files\MATLAB\R2022a\toolbox\matlab\lang\eval)
C:\Program Files\MATLAB\R2022a\toolbox\matlab\lang\@opaque\eval.m % opaque method
"I pulled the whole assignment inside eval" <== that sounds like an enormous mistake. 😬
Walter Roberson
Walter Roberson el 18 de Jun. de 2022
I have to admit that using "syms" is very convenient, especially for creating symbolic functions or arrays of symbols. syms in that form is implemented as assignin('caller') which is a case of creating variables based upon string.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Variables en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

KAE
el 15 de Jun. de 2022

Comentada:

el 18 de Jun. de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by