Changing and Setting the Working Directory

Once you have opened MATLAB, you are always advised to set your working directory to your preferred folder. If this is too technical, it basically means: tell MATLAB where to save your files and where to open them from. If you do not set up a working directory, you might find problems indexing and locating your files.

If you are unsure why this would be necessary, take your internet browser as an example. Say you found a nice recipe book online that you wanted to save to your computer. By default, with most browsers that aren’t configured, downloads will automatically save to your Downloads folder. This is your browser’s default download directory. Similarly with MATLAB, your working directory is like your current folder, to which you can save files and from which you can read files. If you don’t know where to find the Downloads folder, then you can’t access your downloads. The same applies to MATLAB.

The Current Folder window contains a powerful context menu (obtained by right clicking within that window) that is able to do a few things including:

  • Opening the current folder that you are exploring in MATLAB to Windows Explorer on your computer (or the Mac equivalent)
  • Adding files and folders
  • Adding an item to, or removing it from, MATLAB’s path
  • Finding files

To add a path to your working directory, you need to navigate to it using MATLAB’s explorer. Suppose your working directory is located under the path C:\Work. To navigate to that directory, select the icon bounded by red borders as shown in Figure 1.6.

Figure 1.6

Figure 1.6

You will now see a familiar explorer window that you can use to navigate to the folder that you would like to be your working directory, as shown in Figure 1.7.

Figure 1.7

Figure 1.7

Click ‘Select Folder’ and MATLAB should now display all the files and folders included in that directory. As can be seen from Figure 1.8, the MATLAB ‘Current Folder’ is just another explorer that displays the same thing as your OS explorer.

Figure 1.8

Figure 1.8

You can add new files and folders, open files and folders within or outside MATLAB, and many other things. Explore your options!

To add the path to MATLAB’s working directory, right click in the Current Folder window and hover over ‘Add to Path’ in the context menu. As in Figure 1.9, selecting ‘Current Folder’ will add the folder to MATLAB’s directory! You can now use commands in the Command Window that refer to the files to perform operations on/using them.

Figure 1.9

Figure 1.9

Once you have set up a working directory, you may go on to do your tasks in MATLAB a lot easier. You may be aware that by default, Microsoft Word saves its files in .doc or .docx formats, or that text files are saved with .txt formats. MATLAB works with M-files, with .m extensions, but it can also import/export files of other familiar formats!