Using addpath on Compiled Code
28 views (last 30 days)
Show older comments
Commented: Bruno Luong on 16 Jan 2021
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 ()
%% Start connection
%(if not already connected)
boolPIdeviceConnected = false; if ( exist ( 'PIdevice', 'var' ) ), if ( PIdevice.IsConnected ), boolPIdeviceConnected = true; end; end;
if ( ~(boolPIdeviceConnected ) )
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 :
If I check ctfroot on the target computer I get.
So do I just place my the folder I need access to in the ctfroot directory?
If not whats the solution?
Image Analyst on 15 Jan 2021
Edited: Image Analyst on 15 Jan 2021
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:
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:
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.
Bruno Luong on 15 Jan 2021
Edited: Bruno Luong on 15 Jan 2021
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.
Bruno Luong on 16 Jan 2021
Find more on Application Deployment 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!