- 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.
app designer code section doesn't work when compiled as standalone .exe
20 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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:
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.
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!