View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

Be the first to rate this file! 0 Downloads (last 30 days) File Size: 59.4 KB File ID: #40397 Version: 2.0
image thumbnail



Daniel Herber (view profile)


19 Feb 2013 (Updated )

Creates a path to easily save or load your results no matter where your function is located

| Watch this File

File Information

Have you ever forgot to change the working directory and your .mat and .fig files were saved to the wrong location?

Do you want your results and figures saved to a specific folder in the same directory as the function that created them?
What about errors loading data since you did not change your current folder?

Do you move files between computers a lot and to save the results of multiple simulations?

Then mfoldername is the function for you!

This function, mfoldername, derives its name from the MATLAB function mfilename. This submission is modeled after the functionality of mfilename. From their description: "mfilename returns a string containing the file name of the most recently invoked function. When called from within the file, it returns the name of that file. This allows a function to determine its name, even if the file name has been changed."

mfoldername is different by returning a path that is conducive to saving and loading data while still being independent of your function's location (and possibly its name). It has been written to be OS independent. It has an additional option to add an additional directory to the path (and create it if it does not already exist). Please see the submission screenshot for a graphical representation.

*Place at the beginning of your function or script to easily load data or add all subfolders with genpath
*Place near the end of your function or script to easily save your data or figures (see the following link for an example that combines mfoldername and export_fig)

% mfoldername.m
% Creates a path to easily save or load your results no matter where your
% function is located
% [path_name] = mfoldername(fun_path,extra_path)
% fun_path : function path (either dynamic using mfilename('fullpath') or
% static using the name of a function in your current path)
% extra_path: additional directory that you would like to add to the path,
% e.g. 'Saved_Data' so you can save your results in a folder
% named 'Saved_Data' in the same path as your function (similar
% loading data from a specific folder)
% Two general examples:
% 1. You want the current function or script path in new folder
% -The name of this function can change and still function correctly
% -Will create the extra folder for you if it does not already exist
% path_name = mfoldername(mfilename('fullpath'),'Saved_Data');
% 2. You want a specific function or script path in the function folder
% path_name = mfoldername('Test_mfoldername','');

Relevant material:

1. File Name Construction functions (fullfile, fileparts, filesep) -
~these are used to create the path
~thanks Jan for pointing these out!

2. mfilename -
~need to remove the function or script name

3. which -
~need to know the function name
~need to remove the function or script name

4. list name of current FOLDER not full path -
~many issues are discussed
~not for the current file but current working directory

Finally, a short back story on why this function was needed. I had been running a lot of independent optimization routines and at completion, I needed to save the results to a .mat file and number of figures. I did not want to clutter up the function location with many figures and data files so I started saving these items in a folder inside the function folder named 'Saved_Data'. This easily allowed me to navigate to the results and enabled bulk deletion of many runs without having to dodge the necessary functions inside the function location. Additionally, the computing resources that I am able to use are on both Windows and Linux machines. Therefore, I was frequently moving the code from one to another and then running it. This required a new path every time unless it was put in the exact same location as before and two full paths were still required (one for Windows and one for Linux). I wanted to make this entire process easier by creating this function. Later on, this function has also proven useful to load data bundled with the function. This allows for the code and data to be passed from PC to PC and still function correctly.

Note: This submission was originally called msavename (support for msavename is maintained)


This file inspired Basic Implementation Of Multiple Interval Pseudospectral Methods To Solve Optimal Control Problems, Modeling And Design Analysis Of A Permanent Magnet Linear Synchronous Generator, and Pm Architectures Project.

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
11 Jan 2016 Daniel Herber

Daniel Herber (view profile)

Thank you Jan for your comment (yes it is easier). I have completely rewritten the code based on your suggestion.

I wrote this function a couple of years ago after not finding the exact required functionality with the built-in functions although they now seem to have been available. Even though the code is quite concise now, I still believe the function provides a useful tool that can greatly improve your workflow. Hopefully the examples demonstrate this usefulness.

Comment only
04 Jan 2016 Jan Simon

Jan Simon (view profile)

This would be easier:

PathName = fullfile(fileparts(fun_path), extra_path);

This considers Windows/Linux file separators reliably.

Using "path" as name of a variable might cause unexpected effects during debugging, because it is an important Matlab command.

Comment only
10 Jun 2013 1.3

Documentation changes and example to reflect the functions ability to aid with loading data

04 Jan 2016 1.4

Name change to mfoldername (support for msavename is maintained). Documentation updates as well.

04 Jan 2016 1.5

Add missing data file.

11 Jan 2016 2.0

Complete rewrite based on Jan Simon's suggestions

Contact us