Borrar filtros
Borrar filtros

Why are functions called by "eval" not found by my compiled standalone application?

10 visualizaciones (últimos 30 días)
I have a function "databaseConnectWithEval" that is defined as follows:
function conn = databaseConnectWithEval
conn = eval('database("MySQL ODBC","username","password");');
end
If I call this function from the MATLAB Command Window, it works as expected.
However, if I compile this function into a standalone application, and then run the executable, the following error is thrown:
Undefined function 'database' for input arguments of type 'char'.
If I use the following function, without "eval", then both the function and the compiled executable work as expected.
function conn = databaseConnect
conn = database("MySQL ODBC","username","password");
end
Why is this function inside of "eval" not found by my compiled application?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 26 de Jul. de 2024 a las 0:00
When a standalone application is compiled, the dependency analyzer does not parse strings or char arrays in M files, including those inside of "eval".
As a workaround, you may use what we call a "pragma function", which is a pragma that forces the compiler to include the specified function as a dependency. You can find information about the pragma function at the following documentation page.
https://www.mathworks.com/help/compiler_sdk/ml_code/function.html
In this particular case, to use the pragma function simply add the following to the main file of the application.
%#function database
Another approach is to refactor your code to remove the use of "eval". In general, we recommend avoiding the use of "eval" entirely unless absolutely necessary, as it is less efficient and usually makes your code harder to debug.
  1 comentario
John D'Errico
John D'Errico hace alrededor de 23 horas
To expand on the answer, the question is why is stuff inside an eval not found, the parser simply cannot look there. For example, suppose the user created a highly complex string to generate a function name. Even worse, suppose that function name depends on a external file that is loaded at execution time. Or what if the string inside the eval generates a name that is chosen randomly from a list of functions. Or, or, or...
The point is, you can do all sorts of strange things inside an eval, things that cannot be predicted by the parse tool. And if something is possible, then somebody, somewhere, sometime will do it. Therefore, nothing inside an eval will be found as a dependency. They had to draw the line in the sand at that point, not looking inside an eval.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB Compiler en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by