Home > Software design >  Why can't MATLAB find a script that is on MATLABPATH
Why can't MATLAB find a script that is on MATLABPATH

Time:09-30

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?

CodePudding user response:

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.....

CodePudding user response:

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.

  • Related