Using addpath on Compiled Code
35 次查看(过去 30 天)
显示 更早的评论
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
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 个评论
Image Analyst
2021-1-15
编辑: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:
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 个评论
Image Analyst
2021-1-16
编辑: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
2021-1-16
You'll notice the won't use PWD in deployed.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 C Shared Library Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!