Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a command alias in Matlab

Tags:

matlab

Is there a similar concept to Unix 'alias' within Matlab?

This question Is there a way to do command aliasing in matlab R2011b? suggests defining anonymous functions, and extending the answer these could be sourced at startup, but this results in the function handles appearing in the Workspace, which will disappear when cleared.

Is there a more robust and Unix analogous solution? Seems like a pretty useful thing to be able to do...

like image 960
user2846226 Avatar asked Oct 02 '22 18:10

user2846226


1 Answers

I am not sure why you would want to do this, but...

Assuming you are willing to have a directory on the path dedicated to aliases you can create m files in that directory to run the aliases. In this case the aliases will not exist in the workspace. You could of course just write the alias files your self, but the following function will create aliases for you automatically. The function can get confused if the function/script you are trying to alias is not currently on the search path. The function is not "perfect" in the sense that you do not write

alias myAlias = run('full/path/to/some/script')

but rather

alias myAlias full/path/to/some/script

function alias(aliasName, functionName)
% alias myfoo foo

    aliasPath = './alias';
    isscript = false;
    try
        nargin(functionName);
    catch %#ok<CTCH>
        isscript = true;
    end
    
    if isscript
        fileID = fopen([aliasPath, aliasName, '.m'],'w');
        fprintf(fileID, '%s\n', ['run(', functionName, ')']);
        fclose(fileID);
    else
        fileID = fopen([aliasPath, aliasName, '.m'],'w');
        fprintf(fileID, '%s\n', ['function varargout = ', aliasName, '(varargin)']);
        fprintf(fileID, '\t%s\n', ['varargout{1:nargout} = ', functionName, '(varargin{:});']);
        fprintf(fileID, '%s\n', 'end');
        fclose(fileID);
    end
end
like image 122
StrongBad Avatar answered Oct 12 '22 11:10

StrongBad