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

20 次查看(过去 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

采纳的回答

Shaik
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:
  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 个评论
Rajan Prasad
Rajan Prasad 2023-5-15
编辑:Rajan Prasad 2023-5-15
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by