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

msavename(fun_path,extra_path)
%-------------------------------------------------------------------
% msavename.m
% Creates a path to easily save or load your results no matter where your
% function is located
%--------------------------------------------------------------------------
% [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','');
%
%--------------------------------------------------------------------------
% Author: Daniel R. Herber, Graduate Student, University of Illinois at
% Urbana-Champaign
% Date: 2/19/2013
% 06/09/2013 v1.1 Documentation changes and example to reflect the functions 
%                 ability to aid with loading data
%--------------------------------------------------------------------------

function [path] = msavename(fun_path,extra_path)

    % initialize flag and loop counter
    finish_flag = 0;
    n = 0;
    temp_fun_path = fun_path;

    while finish_flag == 0
        found = 0;
        n = n + 1;

        if (n == 3)
            error('Error:FunMissing',...
                strcat(temp_fun_path,' not found!\nCheck if the function is in your path'))
        end
        
        % check locations if windows environment
        for i = 1:length(fun_path)
            found_win(i) = (fun_path(i)=='\');
        end

        % check locations if unix environment
        for i = 1:length(fun_path)
            found_unix(i) = (fun_path(i)=='/');
        end

        % use the correct found data
        if sum(found_win) > 0
            machine_flag = 1; % windows flag
            found = found_win;
            finish_flag = 1; % end loop
        elseif sum(found_unix) > 0
            machine_flag = 2; % unix flag
            found = found_unix;
            finish_flag = 1; % end loop
        else
            fun_path = which(fun_path); % obtain full function path
            % now loop back
        end
    end

    index = find(found); % find locations of slashes
    cutstring = fun_path(1:index(length(index))); % function name removed

    % add saved data folder
    if (machine_flag == 1) && (~strcmp(extra_path,''))
        path = strcat(cutstring,extra_path,'\'); % windows
    elseif (machine_flag == 2) && (~strcmp(extra_path,''))
        path = strcat(cutstring,extra_path,'/'); % unix
    else
        path = cutstring; % if no extra path is set
    end

    % check if the folder exists
    if (exist(path,'dir') == 0) && (~strcmp(extra_path,''))
        mkdir(path) % create the folder
    end

end

Contact us