MATLAB Answers

Using addpath on Compiled Code

83 ビュー (過去 30 日間)
Jason
Jason 2021 年 1 月 15 日
コメント済み: Bruno Luong 2021 年 1 月 16 日
Hello. I have run into a problem using a compiled EXE of my code on a target computer (running the MCR)
On my computer where I create the code (and have Matlab installed) - I succesfully connect to a linear stage
%% Load PI MATLAB Driver GCS2
% (if not already loaded)
addpath ( 'C:\Users\Public\PI\PI_MATLAB_Driver_GCS2' );
if ( ~exist ( 'Controller', 'var' ) || ~isa ( Controller, 'PI_GCS_Controller' ) )
Controller = PI_GCS_Controller ()
end;
%% Start connection
%(if not already connected)
boolPIdeviceConnected = false; if ( exist ( 'PIdevice', 'var' ) ), if ( PIdevice.IsConnected ), boolPIdeviceConnected = true; end; end;
if ( ~(boolPIdeviceConnected ) )
% USB
controllerSerialNumber ='020550023'; %'Enter valid serial number here, e.g. "123456789"'; % Use "devicesUsb = Controller.EnumerateUSB('')" to get all PI controller connected to you PC.
% Or look at the label of the case of your controller
PIdevice = Controller.ConnectUSB ( controllerSerialNumber );
% Query controller identification string
connectedControllerName = PIdevice.qIDN()
Whilst this works fine it doesn't work on my compiled version on the target computer and its throwing an addpath error. Doing some searching I see that I shouldn't use addpath in compiled versions of my code.
So do I need to include all the files in the following folder :
C:\Users\Public\PI\PI_MATLAB_Driver_GCS2\@PI_GCS_Controller
If I check ctfroot on the target computer I get.
C:\Users\Scan\AppData\Local\Temp\Scan\mcrCache9.9\HPBR_60
So do I just place my the folder I need access to in the ctfroot directory?
If not whats the solution?
Thanks
Jason

  0 件のコメント

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2021 年 1 月 15 日
編集済み: Image Analyst 2021 年 1 月 15 日
The problem is the CTF root directory is in some weird place that changes from version to version so you'd have to change it every time you compiled and released a new version.
One problem I recently found out about is that if the end user does not use the program for a long time, then Windows might delete some run-time library functions from your CTF if your CTF is in the "Automatic" folder.
On occasion, some of you may see an error
Undefined function or variable 'matlabrc'.
Undefined function or variable 'ctfroot’.
Error in checkRequiredMCRProducts (line 22)
Undefined function or variable 'ctfroot’.
This shows up in the console window (“DOS box”). Sometimes it might just flash briefly and vanish and the program never starts. The official solution given by Mathworks for this problem is below:
The solution is to delete the CTF extraction folder, and run the EXE again. If the MCR_CACHE_ROOT environment variable is not set, the CTF extraction location can be found at:
C:\Users\[username]\AppData\Local\Temp\[username]\mcrCache[version]
To get around the problem of Windows randomly deleting the files you need to specify that the CTF is in some known, permanent location that Windows won't touch, unlike the temporary folder it normally puts it into. So you can set a Sytemt Environment variable from the control panel:
MCR_CACHE_ROOT=C:\users\public\documents\MATLAB\MCR
Then when you run your program, it will unpack the CTF to that location.
Alternatively, you can put your data into a known place like "C:\Users\Public\Documents\MATLAB\YourData" -- this is what I do. Do you have just a few files or a whole folder of files that changes as the user uses your program?
Have you tried using the -a option in mcc? You can do that if you want to bundle your files to be shipped with your executable and it will always find them. However if you expect to be adding files to that folder, then you'll still need to have a folder where you keep the files that is in a known location, like D:\my data or somewhere and then use fullfile() to construct the full file name of your file whenever you need to use it.

  4 件のコメント

表示 1 件の古いコメント
Jason
Jason 2021 年 1 月 15 日
Thanks I.A. I can't see an option to bring up an environment Variable Control Panel?
So bring up the Environment Variable control panel and make a system level variable called MCR_CACHE_ROOT and set it to some folder you have access to, like
MCR_CACHE_ROOT = C:\Users\Public\Documents\MATLAB\MCR
Image Analyst
Image Analyst 2021 年 1 月 15 日
What operating system are you on? In windows you can type "Environment" into the search field on your task bar, or it you don't have that, just hit the windows key and type environment. You should see the Environment Variables control panel.
If you have a bunch of data files that you want to ship with your app, like file1.txt, etc. and your installer (I use Centurion Setup by Gammadyne company) puts them into 'C:\Users\Public\PI\PI_MATLAB_Driver_GCS2' then you can construct the full filename in your code like this
folder = 'C:\Users\Public\PI\PI_MATLAB_Driver_GCS2'; % Where you KNOW they live. Otherwise ask the use via uigetdir().
baseFileName = 'file1.txt'; % Whatever...
fullFileName = fullfile(folder, baseFileName);
data = importdata(fullFilename); % Whatever function you want.
You do not need to set a path to that folder, EVER.
See the FAQ:
Jason
Jason 2021 年 1 月 15 日
Oh sorry i thought you meant via matlab. Yes i know how to do thiscin windows

サインインしてコメントする。


Bruno Luong
Bruno Luong 2021 年 1 月 15 日
編集済み: Bruno Luong 2021 年 1 月 15 日
I can't see addpath in your snip of code.
When you compile the app, all the source mfiles used by you app MUST be found by dependency analyzer. So you must call appropiate ADDPATH before MCC.
If you app calls ADDPATH, then you just need to remove it and call is elsewhere (outside). You might use isdeployed() to have different code branch. But ideally you should restructure the code so that you would never need to call addpath while your program is running.

  11 件のコメント

表示 8 件の古いコメント
Bruno Luong
Bruno Luong 2021 年 1 月 16 日
You can get the exe file folder if you put this file in the same folder than the main function of you app and call it
function currentDir = getcurrentdir
% currentDir = getcurrentdir
if isdeployed % Stand-alone mode.
[~, result] = system('set PATH');
currentDir = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
else % MATLAB mode.
thisfile = mfilename('fullpath');
currentDir = fileparts(thisfile);
end
I don't know the exact meaning of c.DriverDirectoryOnwindows to make any comment. It might need other stuffs in or under this folder. You should check with whoever who distributes the library.
Image Analyst
Image Analyst 2021 年 1 月 16 日
Is
currentDir = pwd;
not reliable? Actually the current directory can be different than the location of the executable program, like if the program called cd().
Bruno Luong
Bruno Luong 2021 年 1 月 16 日
My code is suggested by Mathworks Support Team here.
You'll notice the won't use PWD in deployed.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by