Transcription
As we write code or use files provided by others,
we’ll want to organize them into separate folders.
But what happens when we try to use these files?
For example,
let’s save this script in our folder “MyProject.”
Let’s try calling our script from the command prompt.
Hmmm…
An error message?
When we call a function or script, MATLAB first searches for this file in the current folder,
before checking an ordered list of folders called the MATLAB Search Path.
Notice in the Current Folder Browser that our folder name appears in a lighter shade,
indicating that the folder is not on the Search Path.
To ensure our file is found, we have two choices.
The first option is to change our Current Folder
to the project folder where the script is stored.
The script name now appears in the Current Folder Browser in black letters,
indicating that it will be found.
Alternatively, if we frequently find ourselves using files from a specific folder,
we can add that folder to the Search Path so that those files will be found
regardless of the Current Folder.
To view and change the Search Path
we use the PATHTOOL command.
Let’s add the folder “MyProject.”
Notice that this folder is now first on our MATLAB Search Path.
To keep this setting
for our current MATLAB session only,
we click CLOSE.
To permanently add this folder for future sessions
we choose SAVE.
And that’s it!
By adding folders to the search path, we can organize our code files the way we want,
and still use them when we want.