How to add user created .mat files to the search path after compilation - matlab

How can I allow user to add additional dependenies after the source code was compiled with mcc.
I was thinking about an empty folder next to the executable, where the users can add the needed .mat-files, but I can't add the folder path to my executable (since addpath is not allowed in deployed applications).
Any ideas?

This answer assumes that your code can be customised by data contained in one or more .mat files at runtime.
You can point your code to look at a folder where the optional .mat file(s) would be located.
For example in the users home folder with a sub folder being the name of your application (or in the local app data) or whereever...
If you want it in a sub folder where the exe is, you can do this as well, you find the exe path using (on windows):
[status, result] = system('path');
installpath = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
fprintf ( 'The exe install path is "%s"\n', installpath );
Then your code looks to load for example:
file2load = fullfile ( installpath, 'subFolder', 'runtimeCustomisation.mat' )
if exist ( file2load, 'file' ) == 2
"doSomething with the file"
end
Or something like that.
Recall that this is for dependencies which are .mat files only.

Related

Why can't MATLAB find a script that is on MATLABPATH

I've just created a script file in MATLAB, but can not run it. The name of my script is getEnvFiles.m. When I first tried to run it, I got the following result:
>> getEnvFiles
'getEnvFiles' is not found in the current folder or on the MATLAB path, but exists in:
\\wsl$\ubuntu\home\me
Change the MATLAB current folder or add its folder to the MATLAB path.
So, I added this directory (which is actually the current directory) to the search path, but still got the same result:
>> addpath('\\wsl$\ubuntu\home\me')
>> getEnvFiles
'getEnvFiles' is not found in the current folder or on the MATLAB path, but exists in:
\\wsl$\ubuntu\home\me
Change the MATLAB current folder or add its folder to the MATLAB path.
When I check the path, it looks like this directory is on the path:
>> path
MATLABPATH
\\wsl$\ubuntu\home\me
I can further verify that this directory is my present directory:
>> pwd
ans =
'\\wsl$\ubuntu\home\me'
and that getEnvFiles.m is in this directory:
>> ls
. .emacs.d HarborData
.. .emacs~ RawHarborData
.bash_history .landscape at
.bash_logout .motd_shown getEnvFiles.m
.bashrc .profile test.m
.bashrc~ .sudo_as_admin_successful
.emacs
Is the issue that I'm using wsl (Windows Subsystem for Linux), or do I have some other misunderstanding?
Type rehash and then try running your script again. Even though you have added the new directory to your path, you need to update the path cache so that it knows about the new scripts it can see.
The problem seems to lie in WSL's ability to add new files to the directory. When I create a new script within MATLAB, and try to run it, I get the problem discussed above. However, running already existing files is not a problem. For now, my only solution is to close MATLAB after creating a new script and reopen it. Then, I can run it. Although, oddly, I can't open it in MATLAB's editor.....
This may or may not help, but ...
Sometimes you have to prepend execution with .\ in order to run scripts in PowerShell or from within the command prompt (in Windows, I'm not sure about other operating systems).
I get this error quite often, especially when not in Admin mode.
Does this work?
% Get path of executing script.
filePath = matlab.desktop.editor.getActiveFilename; % Note that this isn't necessarily the same as the output of 'pwd()'.
% Or: filePath = mfilename('fullpath')
% Make sure all nested directories of filePath are on the path, then tell MATLAB where you're working.
addpath( genpath( filePath ) ); % If this fails, try one folder up the tree:
% addpath( genpath( fullfile( filePath, '.' ) );
cd( filePath );
Alternatively, it looks like you might have written and saved the script, and then typed it into the command window to execute. If it's not a special type of script (Function/Class/GUI/etc.) then you can simply click 'Run' in the Editor tab (or the F5 key) and MATLAB should prompt you with a 'Change Folder' option, to which you should acquiesce.
If you're running this script through the WSL terminal, try my suggested code.

MATLAB is using wrong dependency (files with the same name in different directories)

I'm trying to adapt a MATLAB script. I have the original code in a folder and the new version in another:
dev\original
dev\new
The new folder has all the original files and a few more. But one of the functions dependencies in one of the files in the new folder is calling the file in the original folder by mistake.
File:
dev\new\example.m
Dependency Report:
current dir : file_1
current dir : file_2
other : file_3
Even though there are "file_1", "file_2" and "file_3" in both folders.
dev\original\file_1.m
dev\original\file_2.m
dev\original\file_3.m
dev\new\file_1.m
dev\new\file_2.m
dev\new\file_3.m
Is there a way to force MATLAB to call the correct file? Or maybe only allow it to call files from the current and sub directories?
Edit:
To ilustrate more information, I ran the "pwd" and "which -all" commands to show that MATLAB knows the existence of both files and is running in the "new" folder.
>> pwd
ans =
'C:\dev\new'
>> which -all file_3
Not on MATLAB path % model constructor
C:\dev\new\file_3.m % Shadowed
You should look into using private folders where you control the scope of what your doing,
basically you put file_1, file_2 and file_3 in a private folder:
dev/new/example.m
dev/new/private/file_1.m
dev/new/private/file_2.m
dev/new/private/file_3.m
Then your example will call the file_*.m in the appropriate private folder.

Zip files using 7zip from command window

Using 7zip, I want to give a location of a folder of files something like...
D:\Home\files Then I want it to zip all the files in that folder and leave them there. So for example zipme.txt become zipme.zip, so all files would keep their name but just become a zip file. I have tried to use
FOR %i IN (D:\Home\files) DO 7z.exe a "%~ni.zip"
But when I do it it adds a zip file for the directory so my output would be in the correct folder but would contain
D:\Home\files\file.zip
D:\Home\files\zipme.zip
zipped files also all items in directory like..
zipme.txt
zipme2.txt
D:\Home\files/zipme2.zip
So how can I zip each file individual in a folder and have the new zipped name be the individual files name
Was able to get this to work.
FOR %i IN (D:\Home\files\*) DO 7z.exe a "%~ni.zip" "%i"

Load file in matlab ralative to script location

I have a simple script in matlab and I want to load a file. It seems it only works if the file is in the same dir as the script. If I add the file to a directory it does not read it.
For example:
fileID = fopen('myfile','r' ,'n', 'US-ASCII');
but when I put myfile in files:
fileID = fopen('files/myfile','r' ,'n', 'US-ASCII');
or
fileID = fopen('./files/myfile','r' ,'n', 'US-ASCII');
I get a -1 as a fileID. File cannot be read.
As per the comments, this is happening because you most likely added the path of where the script was located to your MATLAB path but you did not add the subdirectory where the file was in to your path. This is why it can't find the file. Therefore to avoid this in the future, you need to physically change the directory (i.e. the Working Directory) of where MATLAB is currently operating to where your script is stored.
It is then where local referencing should work. You can do this by either using the cd function, going to the top of your MATLAB window where you see the directory listing, clicking on the arrow to the right and pulling a drop down menu to change the directory, typing the actual directory you want by clicking on any blank space in the directory listing to enable a text box:
... or if you are running the code in the MATLAB editor, it'll request that you change directories as the script you are trying to run is not currently located in the working directory.
You can also programmatically add the subfolders in your script directory using mfilename, fileparts, genpath and addpath:
[dir, ~, ~] = fileparts(mfilename('fullpath')); % locate your script directory
addpath(genpath(fullfile(dir))); % add the folder and all subfolders to Matlab search directory
% then load your file.
fileID = fopen('myfile','r' ,'n', 'US-ASCII')
If it is also important that all outputs should be placed within the same directory as your script file, you can cd to your script directory:
cd(dir)

Matlab function call directory

So i had this issue that occurred when I ran a Matlab script. Here is an a simple example that illustrates it:
So its important to outline the file structure:
MainFolder
script.m
SubFolder
a1.csv
a2.csv
a3.csv
now say i have a script like this:
-> script.m
dir
it would simply print out the files in the folder.
Now the wierd thing, if i run the script in the Subfolder like this:
>>script
it will do this:
>> a1.csv a2.csv a3.csv
but if i do this in the folder:
>>run('C:\Users\....\MainFolder\script.m')
it will only print out
>> script.m
So obviously it is acting as if i ran it form MainFolder rather than SubFolder.
What is the point of this functionality?
The dir command shows the directory contents of Matlab's current directory, not that of where the script is located. So the script showed you the directory contents of wherever you happened to be in the Matlab command prompt when you called that script.
To get what you want, use this in the script:
dir(fileparts(mfilename('fullpath')))
Use pwd to see current dir
Use cd to change directory
Use path to see if your project folders are included in the path
Use which to see you are calling the right *.m file (in case there is multiple .m files with same name on the path)