# Creating a Custom Function

As we have seen MATLAB contains a large number of very useful inbuilt functions. We can however also create our own function. This is useful if you are continually needing to calculate, compute or plot something in the same way with different input data.

# Function Name, Inputs, Outputs and the Functions Own Internal Workspace

All functions have the form:

<span style="color: #0000ff;">function</span> <span style="color: #ff0000;">[</span>output1<span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span>input1,input2<span style="color: #33cccc;">)</span>

<span style="color: #0000ff;">end</span>

For the function to be called up it's filename has to match the specified name of the function, in our case

<span style="color: #00ffff;"><span style="color: #33cccc;">functionname</span></span>.m

Before creating a function, it is useful to check if the function name exists. We can use exist:

<span style="color: #0000ff;">exist</span> <span style="color: #33cccc;">functionname</span>

We see this doesn't exist so we can go ahead and create it.

<span style="color: #0000ff;">function</span> <span style="color: #ff0000;">[</span>output1<span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span>input1,input2<span style="color: #33cccc;">)</span>

<span style="color: #0000ff;">end</span>

Let's modify the above so the function that evaluates output1 and output2. Note the code contained within the function is indented so it is easier to follow. The indention indicates that the start

<span style="color: #0000ff;">function</span>

corresponds to the end

<span style="color: #0000ff;">end</span>

and any line of code that is indented is contained within the function:

<span style="color: #0000ff;">function</span> <span style="color: #ff0000;">[</span>output1<span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span>input1,input2<span style="color: #33cccc;">)</span>
output1=2
output2=4
<span style="color: #0000ff;">end</span>

Here there is no dependence on the input values so it doesn't matter what we type in the Command Window.

<span style="color: #ff0000;">[</span>output1<span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span>1,1<span style="color: #33cccc;">)</span>
<span style="color: #ff0000;">[</span>output1<span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span>1,2<span style="color: #33cccc;">)</span>
<span style="color: #ff0000;">[</span>output1<span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span>2,<span style="color: #800080;">'</span>MATLAB<span style="color: #800080;">'</span><span style="color: #33cccc;">)</span>

We will get the same results irregardless of the inputs. We can even skip the inputs by using

<span style="color: #ff9900;">[ ]</span>

or just not mention them:

<span style="color: #ff0000;">[</span>output1<span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span><span style="color: #ff9900;">[ ]</span>,<span style="color: #ff9900;"><span style="color: #800080;"><span style="color: #ff9900;">[</span> <span style="color: #ff9900;">]</span></span></span><span style="color: #33cccc;">)</span>
<span style="color: #ff0000;">[</span>output1<span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname</span></span>

Like most inbuilt functions we can re-specify the output names

<span style="color: #ff0000;">[</span>A<span style="color: #808080;">,</span>B<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span>2,<span style="color: #800080;">'</span>MATLAB<span style="color: #800080;">'</span><span style="color: #33cccc;">)</span>

i.e. we don't need to restrict ourselves to the output names used within the function. The order we list them however will correspond to the order they are listed in the function. Recall we can ignore a variable by using

<span style="color: #ff9900;">[</span> <span style="color: #ff9900;">]</span>

<span style="color: #ff0000;">[</span>C<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span>2,<span style="color: #800080;">'</span>MATLAB<span style="color: #800080;">'</span><span style="color: #33cccc;">)</span>

<span style="color: #ff0000;">[</span><span style="color: #ff9900;">[]</span><span style="color: #808080;">,</span>D<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span>2,<span style="color: #800080;">'</span>MATLAB<span style="color: #800080;">'</span><span style="color: #33cccc;">)</span>

Okay so now lets create a variable in the workspace:

x=4
y=8

Now let's also modify the function to create Variable

x
<span style="color: #0000ff;">function</span> <span style="color: #ff0000;">[</span>output1<span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span>input1,input2<span style="color: #33cccc;">)</span>
output1=2;
output2=4;
x=2
<span style="color: #0000ff;">end</span>

We will see after running this function that the variable

x

in the workspace remains unchanged… although

x

was assigned to 2 in the function… Why is this?

This is because the function contains it's own independent internal workspace and the only way a variable can cross over from the MATLAB workspace to the functions

<span style="color: #00ffff;"><span style="color: #33cccc;">functionname</span></span>

workspace is via the use of inputs, likewise the only way a variable can cross over from the functions

<span style="color: #00ffff;"><span style="color: #33cccc;">functionname</span></span>

workspace to the MATLAB workspace is via the use of outputs. If we modify the function so we use an input:

<span style="color: #0000ff;">function</span> <span style="color: #ff0000;">[</span>output1<span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span><span style="color: #ff6600;">input1</span>,input2<span style="color: #33cccc;">)</span>
output1=2;
output2=4;
x=2;
<span style="color: #ff6600;">input1</span>;
fprintf(<span style="color: #993366;">'</span>input1 is <span style="color: #ff0000;">%d</span><span style="color: #993366;">'</span>, <span style="color: #ff0000;">input1</span>)
<span style="color: #0000ff;">end</span>

In essence when we type the function name with inputs in the Command Window:

<span style="color: #00ffff;"><span style="color: #33cccc;"><span style="color: #ff0000;">[</span></span></span>A<span style="color: #00ffff;"><span style="color: #33cccc;"><span style="color: #808080;">,</span></span></span>B<span style="color: #00ffff;"><span style="color: #33cccc;"><span style="color: #ff0000;">]</span></span></span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span><span style="color: #ff6600;">3</span>,<span style="color: #ff00ff;">4</span><span style="color: #33cccc;">)</span>

The first thing MATLAB does within the functions internal workspace is assign the variable names to the inputs we specified:

<span style="color: #ff6600;">input1<span style="color: #000000;">=</span>3</span>;
<span style="color: #ff00ff;">input2<span style="color: #000000;">=</span>4</span>;

It then carries out the code within the unction and then quits when it finds the function

<span style="color: #00ffff;"><span style="color: #33cccc;">functionname</span></span>

when it finds the

<span style="color: #0000ff;">end</span>

statement.

To update

x

we would use the output name

x
<span style="color: #0000ff;">function</span> <span style="color: #ff0000;">[</span><span style="color: #99cc00;">output1</span><span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span><span style="color: #ff6600;">input1</span>,input2<span style="color: #33cccc;">)</span>
<span style="color: #99cc00;">output1</span>=<span style="color: #ff6600;">input1</span>;
output2=4;
fprintf(<span style="color: #993366;">'</span>input1 is <span style="color: #ff0000;">%d</span><span style="color: #993366;">'</span>, <span style="color: #ff0000;">input1</span>)
<span style="color: #0000ff;">end</span>

Typing the following into the Command Window:

<span style="color: #ff0000;">[</span><span style="color: #99cc00;">x</span><span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span><span style="color: #ff6600;">1</span>,3<span style="color: #33cccc;">)</span>

Now that the function requires the first input in order to execute the code.

<span style="color: #0000ff;">function</span> <span style="color: #ff0000;">[</span>output1<span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span><span style="color: #ff6600;">input1</span>,input2<span style="color: #33cccc;">)</span>
<span style="color: #99cc00;">output1</span>=<span style="color: #ff6600;">input1</span>;
output2=4;
fprintf(<span style="color: #993366;">'</span>input1 is <span style="color: #ff0000;">%d</span><span style="color: #993366;">'</span>, <span style="color: #ff0000;">input1</span>)
<span style="color: #0000ff;">end</span>

Calling this function without this input will result in an error as the code cannot execute without it:

<span style="color: #ff0000;">[</span><span style="color: #99cc00;">x</span><span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span><span style="color: #ff6600;">[]</span>,3<span style="color: #33cccc;">)</span>
<span style="color: #ff0000;">[</span><span style="color: #99cc00;">x</span><span style="color: #808080;">,</span>output2<span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname</span></span>

We can now update the function to reference both inputs in the functions code and update both the outputs. In this simple case, we will just assign the outputs to the inputs:

<span style="color: #0000ff;">function</span> <span style="color: #ff0000;">[</span><span style="color: #99cc00;">output1</span><span style="color: #808080;">,</span><span style="color: #ff00ff;">output2</span><span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span><span style="color: #ff6600;">input1</span>,<span style="color: #cc99ff;">input2</span><span style="color: #33cccc;">)</span>
<span style="color: #99cc00;">output1</span>=<span style="color: #ff6600;">input1</span>;
<span style="color: #ff00ff;">output2</span>=<span style="color: #cc99ff;">input2</span>;
fprintf(<span style="color: #993366;">'</span>input1 is <span style="color: #ff0000;">%d</span><span style="color: #993366;">'</span>, <span style="color: #ff0000;">input1</span>)
<span style="color: #0000ff;">end</span>

Typing the following into the Command Window, will update the Variables

x

and

y

in accordance with the two inputs we assigned when calling the function within the Command Window:

<span style="color: #ff0000;">[</span><span style="color: #99cc00;">x</span><span style="color: #808080;">,</span><span style="color: #ff00ff;">y</span><span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">functionname(</span></span><span style="color: #ff6600;">1</span>,<span style="color: #cc99ff;">3</span><span style="color: #33cccc;">)</span>

# A Simple Function: Unit Converter

Supposing we want to make a Unit Converter to convert from inches to cm using the conversion factor 2.54. We can call this

<span style="color: #00ccff;">inch2cm</span>

.

<span style="color: #0000ff;">function</span> <span style="color: #ff0000;">[</span><span style="color: #cc99ff;">output_cm</span><span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">inch2cm(</span></span><span style="color: #ff6600;">input_inch</span><span style="color: #33cccc;">)</span>
<span style="color: #339966;">% This simple function will convert the input in inches to the output in cm using the conversion factor 2.54.</span>
<span style="color: #cc99ff;"> output_cm</span>=2.54*<span style="color: #ff6600;">input_inch</span>;
<span style="color: #0000ff;">end</span>

# A Modified Function: Random Number Generator

Custom functions can call up already inbuilt functions. Supposing we want to make a random number generator from a minimum negative value to positive maximum value.

<span style="color: #0000ff;">function</span> <span style="color: #ff0000;">[</span><span style="color: #000000;">M</span><span style="color: #ff0000;">]</span>=<span style="color: #00ffff;"><span style="color: #33cccc;">randi2(<span style="color: #000000;">min,max,m,n</span></span></span><span style="color: #33cccc;">)</span>
<span style="color: #339966;">% This will generate random integers between a negative minimum and positive maximum value</span>
M=randi(max-min+1,m,n)-min-1
<span style="color: #0000ff;">end</span>

This site uses Akismet to reduce spam. Learn how your comment data is processed.