app designer code section doesn't work when compiled as standalone .exe

The code works perfectly fine with app desinger when run with matlab or from app designer iteself.
However, when the app is build as a standalone .exe, the following section fails to read the function from user defined location.
[file,path] = uigetfile('*.m','Select Cable Definition File',cd);
cDir = cd;
cd(path);
try
func_name = str2func(erase(file,'.m'));
Cable = func_name();
cd(cDir);
catch ME
sprintf('>> Error : [ %s].',ME.message)
end
The sample function I tried to read is also attached.
function [Cable] = CableDefinition()
% function [Cable] = CableDefinition()
% CableDefinition function contains definitions of all cables,
% JointRelation , Cable Names, etc.
%% Define cables
CT.Anchor = {'Hip_F1','Thigh_U_F'};
CT.AnchorZone = {'Torso','Thigh'};
CT.F = 1;
CT.JointRelation = [1 0];
CT.Location = {'Torso','Thigh'};
CT.Name = {'1'};
CT.JointCenter = {'Hip'};
CT.Routing = {'Posterior'};
Cable.Cable1 = CT;
end

 Respuesta aceptada

Shaik
Shaik el 14 de Mayo de 2023
Hi,
The issue you're experiencing with the code when running the standalone .exe file could be due to the way the MATLAB Compiler (used to create the standalone .exe) handles dynamically loading functions from user-defined locations.
When the MATLAB Compiler builds the standalone application, it includes only the required functions and dependencies that are specified in the MATLAB code. It does not include any additional functions that are dynamically loaded at runtime, such as the function selected by the user using uigetfile in your case.
To resolve this issue, you can try the following approach:
  1. Instead of dynamically loading the function using str2func and func_name(), you can modify your code to use eval to execute the code of the selected function directly.
With this modification, the selected function code will be read from the file using fileread, and then eval will execute the code in the current MATLAB workspace.
Make sure to update the rest of your code to use the variables and structures defined by the selected function.
Note: Using eval can have security implications if the input files are not trusted. Ensure that you only load trusted files or add appropriate input validation and error handling mechanisms.
I hope this helps resolve the issue with loading the function when running the standalone .exe file.

7 comentarios

I have tried both 'eval' and 'evalin'. These alll function works fine with matlab app designer.
But all fails when I create the standalone .exe file.
I have tried something like this:
Cable = eval(erase(file,'.m'));
When creating a standalone executable file from MATLAB, there are some limitations and considerations regarding the use of eval or evalin. These functions might not work as expected in the standalone executable due to the way the code is compiled and executed.
Instead of using eval or evalin to dynamically execute code from a file, a recommended approach is to use function handles or function pointers. Instead of evaluating the code directly, you can define a function in a separate file and use a function handle to access it.
Here's an example of how you can modify your code to use function handles:
  1. Create a separate MATLAB function file, let's call it getCableDefinition.m, with the following content:
function Cable = getCableDefinition()
% CableDefinition function contains definitions of all cables,
% JointRelation , Cable Names, etc.
% Define the cable properties here
CT.Anchor = {'Hip_F1', 'Thigh_U_F'};
CT.AnchorZone = {'Torso', 'Thigh'};
CT.F = 1;
CT.JointRelation = [1 0];
CT.Location = {'Torso', 'Thigh'};
CT.Name = {'1'};
CT.JointCenter = {'Hip'};
CT.Routing = {'Posterior'};
Cable.Cable1 = CT;
end
2. In your main script, modify the code to use a function handle:
file = 'getCableDefinition.m'; % or use the uigetfile function to get the file name
func_handle = str2func(erase(file, '.m'));
Cable = func_handle();
By using a function handle, you avoid the need to evaluate the code dynamically. This approach should be compatible with creating a standalone executable using MATLAB Compiler or MATLAB Compiler SDK.
Please note that if you have other dynamic code evaluation requirements in your application, you may need to rethink your approach and consider alternative solutions that are compatible with standalone executables.
Rajan Prasad
Rajan Prasad el 14 de Mayo de 2023
Editada: Rajan Prasad el 14 de Mayo de 2023
This is exactly the same I tried earlier as an alternative to eval and it did not work.
Still its facing the problem. In standalone, it fails to read function handle via str2func or eval.
[file,path] = uigetfile('*.m','Select Cable Definition File',cd);
cDir = cd;
cd(path);
try
func_name = str2func(erase(file,'.m'));
Cable = func_name();
cd(cDir);
catch ME
sprintf('>> Error : [ %s].',ME.message)
end
Hi, Sorry to hear the issue, check these factors once,
In the code you provided, you're using the str2func function to convert the file name (without the .m extension) into a function handle, and then calling that function to retrieve the Cable variable. If this approach is not working, there could be a few reasons for it.
  1. Make sure that the file you're selecting with uigetfile is in the same directory as your code or in a directory included in the MATLAB path.
  2. Verify that the file you're selecting with uigetfile is a valid MATLAB function file with the .m extension.
  3. Ensure that the function you're trying to call is defined correctly within the selected file. It should have the same name as the file (without the .m extension) and should return the Cable variable.
I have tried, but its not working.
I am getting following error.
>> Error : [ Unrecognized function or variable 'CableDefinition'.].
I have put the CableDefinition.m file in the sam directory.
This is the code i have used while builidng the standalone app.
[file,path] = uigetfile('*.m','Select Cable Definition File',cd);
try
func_name = str2func(erase(file,'.m'));
app.Cable = func_name();
app.StatusTextArea.Value{end+1} = sprintf('>> The selected Cable Def file is %s.',file);
catch ME
app.StatusTextArea.Value{end+1} = sprintf('>> Error : [ %s].',ME.message);
end
Rajan Prasad
Rajan Prasad el 15 de Mayo de 2023
Editada: Rajan Prasad el 15 de Mayo de 2023
Thanks, will check .
However, the problem only persist with standalone function. The same code works fine when run in matlab app designer. So i guess the problem is not with the intialization or definition, something more related to eval or str2func or reading file from the folder.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos

Versión

R2021b

Preguntada:

el 14 de Mayo de 2023

Editada:

el 15 de Mayo de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by