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

20 views (last 30 days)
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

Accepted Answer

Shaik
Shaik on 14 May 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 Comments
Rajan Prasad
Rajan Prasad on 15 May 2023
Edited: Rajan Prasad on 15 May 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.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!