Run Functions in the Editor

This example shows how to run a function that requires some initial setup, such as input argument values, while working in the Editor.

  1. Create a function in a program file named myfunction.m.

    function y = myfunction(x)
    y = x.^2 + x;

    This function requires input x.

  2. View the commands available for running the function by clicking Run on the Editor tab. The command at the top of the list is the command that the Editor uses by default when you click the Run icon.

  3. Replace the text type code to run with an expression that allows you to run the function.

    y = myfunction(1:10)

    You can enter multiple commands on the same line, such as

    x = 1:10; y = myfunction(x)

    For more complicated, multiline commands, create a separate script file, and then run the script.


    Run commands use the base workspace. Any variables that you define in a run command can overwrite variables in the base workspace that have the same name.

  4. Run the function by clicking Run or a specific run command from the drop-down list. For myfunction.m, and an input of 1:10, this result appears in the Command Window:

    y =
         2     6    12    20    30    42    56    72    90   110

    When you select a run command from the list, it becomes the default for the Run button.

To edit or delete an existing run command, select the command, right-click, and then select Edit or Delete.