Functions are program files that accept inputs and return outputs. To run functions
that require input argument values or any other additional setup from the Editor,
configure the
Run button.
To configure the Run button in the Editor, click
Run
and add one or more run commands.
For example:
Create the function myfunction.m
that performs a
calculation using the inputs x
and y
and
stores the results in z
.
function z = myfunction(x,y)
z = x.^2 + y;
Go to the Editor tab and click Run
. MATLAB® displays the list of commands available for running the
function.
Click the last item in the list and replace the text type code to
run with a call to the function including the required input
arguments. For example, enter the text result =
myfunction(1:10,5)
to run myfunction
with the
input arguments 1:10
and 5
, and store the
results in the variable result
. MATLAB replaces the default command with the newly added command.
To run multiple commands at once, enter the commands on the same line. For
example, enter the text a = 1:10; b = 5; result =
myfunction(a,b)
to create the variables a
and
b
and then call myfunction
with
a
and b
as the input arguments.
Note
If you define a run command that creates a variable with the same name as a variable in the base workspace, the run command variable overwrites the base workspace variable when you run that run command.
Click the
Run button. MATLAB runs the function using the first run command in the list. For
example, click
Run to run
myfunction
using the
command result = myfunction(1:10,5)
. MATLAB displays the result in the Command Window.
result = 6 9 14 21 30 41 54 69 86
To run the function using a different run command from the list, click
Run
and select the desired command. 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, click Run
, right-click the command, and then select
Edit or Delete.
Note
Running live functions using the
Run button is not supported in the Live Editor. To run a live
function, call it from the Command Window or from a script or live script.