Be the first to rate this file! 11 Downloads (last 30 days) File Size: 3.06 KB File ID: #40397
image thumbnail

msavename

by

 

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
Description

This function, msavename, 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."

msavename 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
*Place near the end of your function or script to easily save your data or figures

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 had 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.

%--------------------------------------------------------------------------
% [path] = msavename(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 = msavename(mfilename('fullpath'),'Saved_Data');
%
% 2. You want a specific function or script path in the function folder
%
% path = msavename('Test_msavename','');
%
%--------------------------------------------------------------------------

Similar MATLAB functions

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

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

Acknowledgements

This file inspired Modeling And Design Analysis Of A Permanent Magnet Linear Synchronous Generator.

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Updates
10 Jun 2013

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

Contact us