0

Don't know why this isn't working anymore. Very simple. I have a script with a folder in the same path. The folder contains a series of m files for the script to work.

Originally I simply would use

addpath('.../utilities/);

when the script was first run. but recently I started receiving this error

Warning: Name is nonexistent or not a directory: ...\utilities

In path (line 109)

In addpath (line 88)

In Myrunningcode (line 101)

and I don't know why.

I fixed the problem by running the following code

p = mfilename('fullpath');
[filepath,~,~] = fileparts(p);
addpath([filepath,'/utilities/']);

At least I would like to know why this error occurred.

Here is my directory setup. I use windows 10 and matlab 2016a. enter image description here

5
  • What changed? OS? MATLAB version? Commented Nov 14, 2018 at 9:59
  • nothing changed from what I can tell. I'm using matlab 2016a on windows 10. Commented Nov 14, 2018 at 10:02
  • 1
    Use fullfile instead of manually concatenating with (OS dependent) file separators. If your error is a direct copy of the actual error, it would appear you've used a backslash where your example cites a forward slash, this inconsistency is a red flag. The issue is likely that your current directory (pwd) is not the same as the file location (perhaps it was when your code worked) - the relative directory isn't relative to the current script, it's relative to pwd, hence why the mfilename workaround fixes your issue. Commented Nov 14, 2018 at 10:07
  • Is there a simpler method to adding a folder based on script location or is my workaround good enough for what I am doing? As a side question, Is it possible to know if a folder path was added to the list? I don't think it is wise to call addpath every time I run the script. Commented Nov 14, 2018 at 10:13
  • @Wolfie no, I use this in both linux and windows. in Windows, if you do addpath('.../utilities/); you will get a warning using ...\utilities, as it knows you mean that. Using / will make the code OS-independent. Commented Nov 14, 2018 at 10:16

2 Answers 2

3

The issue is likely that your current directory (pwd) is not the same as the file location. The relative directory isn't relative to the current script, it's relative to pwd, hence why the mfilename workaround fixes your issue.

The first solution is your own, but you can do it in one line:

addpath( fullfile( fileparts( mfilename('fullpath') ), 'utilities' ) );

Then the quickest way to check if your files are already on the path is using which:

% Assuming that myFile.m is within the utilities folder, and not shadowed elsewhere.
% If utilities is on the path, which('myFile') will not be empty.
if isempty( which( 'myFile' ) )
    addpath( fullfile( fileparts( mfilename('fullpath') ), 'utilities' ) );
end

Alternatively, you could pair the above check with a persistent flag variable, so you don't have to repeat the check if you re-enter the function.

Note that addpath isn't particularly slow, it's genpath you want to avoid if you were to add a load of subdirectories too.

Aside: It's good to use fullfile instead of manually concatenating with (OS dependent) file separators. Less room for error (e.g. double slashes) even if you're always using the same OS.

Sign up to request clarification or add additional context in comments.

3 Comments

But as a note: addpath('./utilities/'); is OS independent. I agree with you with the timing of addpath. Its probably better to just add it to path multiple times than to check which.
@AnderBiguri Not disputing that, I'm suggesting the use of fullfile to avoid the [filepath,'/utilities/'] which the OP used, assuming relative paths won't work because the user in the wrong location anyway.
Certainly this is a good answer for what OP needs ;). One could argue that you may want to do a different thing by design, i.e. you may want to force the user of the script to be in the path above the folders, but its unrelated to the question and its case-specific. This answer is better than mine because it solves the problem that OP has I think ;)
2

The correct way to include a relative folder is:

addpath('./utilities/');

with a single dot.

This has worked (and works) since the existence of relative folders, AFAIK, so you should be able to use it without fear of deprecation

2 Comments

I tried this but the error still persists. matlab claims the folder doesn't exist even though I can see it in the directory next to the m-file. If I give a full path than the path is added with no problems.
@Hojo.Timberwolf Is your code running in the upper folder to that one? I mean, I have used this in MATLAB 2014,2016,2017,2018, Windows 7,8,8.1,10, Ubuntu 16,17,18. It works. You need to be running the code in the folder that is directly above ./utilities though, as that is how a relative path is defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.