MATLAB Compiler 不支持修改搜索路径 - 小众知识

MATLAB Compiler 不支持修改搜索路径

2023-03-22 07:25:00 苏内容
  标签: MATLAB


I call  matlab scripts in a C++ application using the generic interface  and that works quite well.

But, if my main script (main.m) locate in the folder A calls a function (foo) defined in  foo.m, and  located in  folder B, then I got an error such as
                           Undefined function or variable 'foo'.
I tried to define the environemnt variable MATLABPATH and also to modify pathdef.m,  without success .
Is there a way to tell Matalb where to look for user's function ?

Hi, there,
I encountered a problem when calling a standalone matlab porgram from a C program. I appreciate for any help.
Here is the C code:
The following is the original .m code of the Matlab standalone program "Dist.exe".
Note that MATPOWER4.0 is a package of MATLAB for solving power flow in power system, and runpf and loadcase are both functions included in MATPOWER 4.0.
function Dist
if ~isdeployed
addpath('C:\Program Files\matpower4.0');
addpath('C:\Program Files\matpower4.0\t');
results = runpf('case30', mpoption('PF_ALG', 1));
afterDis = loadcase('case30');
The Command Prompt shows :
Error using==>loadcase at 244
loadcase: specified case not in MATLAB's search path
Error iin==>runpf at 123
It is obviously something wrong with addpath function. Although I have added if ~isdeployed , the problem is still there.

Adding the "if ~isdeployed" actually prevents the addpath commands from running in Dist.exe, because isdeployed returns true from the compiled application. However, even if you got rid of the "if" statement, the recommended way to add a directory to the MATLAB path is to actually package the required toolbox (MATPOWER4.0) files into the executable's CTF archive by using the "-a" option with the mcc command. You can now access this in the compiled executable by using a path relative to "ctfroot". For example, if your toolbox files are in a directory called "matpower4.0", you can use:
if ~isdeployed
addpath('C:\Program Files\matpower4.0');
addpath(fullfile(ctfroot, 'matpower4.0');
See Can I add all sub-directories within a directory to the include path in the MCC command in MATLAB Compiler? and Can I use ADDPATH in a compiled application from MATLAB Compiler? for more information.

HI, Kaustubha Govind,
Thanks for the help! It works.
I found out I forgot to include the matpower4.0 into CTF archive. I changed the mcc instruction while compiling my program:
mcc -m Dist.m -o Dist -a ./matpower4.0
Thanks a lot!!

I have created one app which takes input as a path and if you press the button it loads specific .mat file from that path.
This is how app looks like
 When I run .exe / standalone app , it gives following error in cmd.
'Error using matlabpath
Modifying the search path is not supported by MATLAB Compiler. Remove functions that modify the search path from your MATLAB code. To make files visible to your deployed application, add the parent folder to your MATLAB session.'
How to get ride of this issue ? or is it not possible in standalone app ?
this is how main part of code look like

You should not use the 'addpath' command (static) while creating the standalone executable file. Instead you can the get the directry information using uigetdir() as a dynamic. It avoids the error.

© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1