- 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
19 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Shaik
2023-5-14
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!