skip to Main Content

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$ubuntuhomeme

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$ubuntuhomeme')
>> getEnvFiles
'getEnvFiles' is not found in the current folder or on the MATLAB path, but exists in:
    \wsl$ubuntuhomeme

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$ubuntuhomeme

I can further verify that this directory is my present directory:

>> pwd

ans =

    '\wsl$ubuntuhomeme'

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?

4

Answers


  1. Chosen as BEST ANSWER

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


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

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search