user2846226
user2846226

Reputation: 447

how to create a command alias in 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...

Upvotes: 5

Views: 4994

Answers (2)

mr. cooper
mr. cooper

Reputation: 880

Yes there is a way. It's called a function. You just write a function to do whatever you want the alias to do. For example:

function cdhome
    cd(getenv('MATLABUSERPATH'))

Then just type cdhome at the command line, exactly like an alias in a unix shell. Note that MATLABUSERPATH is an environment variable I define in startup.m. It can be easier, just save a script called cdhome.m with the following content:

cd(getenv(<ENVIRONMENT_VARIABLE_THAT_DEFINES_MY_FAVORITE_PATH>))

Or how about:

clc; clear; close all;

Save that in a script file called clean.m and then simply put clean at the top of your scripts instead of clc; clear; close all;

No, it doesn't exactly follow the unix idea of having a bunch of alias's in a single configuration file that executes on startup.

But you can do that too. Put all your aliases in a switch block, in one function, call it alias:

    function alias(thecalledalias)
        switch thecalledalias
            case somealias
                % ... define some alias
            case someotheralias
                % ... define some other alias
         end

For autocomplete, which you would get with your shell, put a functionSignatures.json file in the same folder as the alias.m function file and populate it with your aliases:

{
   "_schemaVersion": "1.0.0",
   "alias":
   {
      "inputs":
      [
         {"name":"thecalledalias", "kind":"required", 
         "type":["char", "choices={'somealias','someotheralias'}"]}
      ]
  }
}

Then you will get autocomplete when you type alias(...) at the command line.

But to me it is easier to have one folder, call it myaliases with one script or function for each alias, and you get autocomplete at the command line just like when you call any function (i.e., no need to type alias(<somealias>)).

Upvotes: 0

StrongBad
StrongBad

Reputation: 879

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

Upvotes: 4

Related Questions