mfoldername

Creates a path to easily save or load your results no matter where your function is located
605 Downloads
Aktualisiert 11. Jan 2016

Lizenz anzeigen

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)
http://www.danielherber.com/matlab.php?option=post_3

%-------------------------------------------------------------------
% 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) - http://www.mathworks.com/help/matlab/file-name-construction.html
~these are used to create the path
~thanks Jan for pointing these out!

2. mfilename - http://www.mathworks.com/help/matlab/ref/mfilename.html
~need to remove the function or script name

3. which - http://www.mathworks.com/help/matlab/ref/which.html
~need to know the function name
~need to remove the function or script name

4. list name of current FOLDER not full path - https://www.mathworks.com/matlabcentral/newsreader/view_thread/284853
~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)

Zitieren als

Daniel R. Herber (2024). mfoldername (https://www.mathworks.com/matlabcentral/fileexchange/40397-mfoldername), MATLAB Central File Exchange. Abgerufen.

Kompatibilität der MATLAB-Version
Erstellt mit R2012b
Kompatibel mit allen Versionen
Plattform-Kompatibilität
Windows macOS Linux
Kategorien
Mehr zu Search Path finden Sie in Help Center und MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Veröffentlicht Versionshinweise
2.0.0.0

Complete rewrite based on Jan Simon's suggestions

1.5.0.0

Add missing data file.

1.4.0.0

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

1.3.0.0

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

1.0.0.0