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:

function [output1,output2]=functionname(input1,input2)

 

end

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

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

exist functionname

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

function [output1,output2]=functionname(input1,input2)

 

end

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 function corresponds to the end end and any line of code that is indented is contained within the function:

function [output1,output2]=functionname(input1,input2)

   output1=2

   output2=4

end

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

[output1,output2]=functionname(1,1)

[output1,output2]=functionname(1,2)

[output1,output2]=functionname(2,'MATLAB')

We will get the same results irregardless of the inputs. We can even skip the inputs by using [ ] or just not mention them:

[output1,output2]=functionname([ ],[ ])

[output1,output2]=functionname

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

[A,B]=functionname(2,'MATLAB')

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 [ ]

[C]=functionname(2,'MATLAB')

 

[[],D]=functionname(2,'MATLAB')

 

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

function [output1,output2]=functionname(input1,input2)

   output1=2;

   output2=4;

   x=2

end

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 functionname workspace is via the use of inputs, likewise the only way a variable can cross over from the functions functionname workspace to the MATLAB workspace is via the use of outputs. If we modify the function so we use an input:

function [output1,output2]=functionname(input1,input2)

   output1=2;

   output2=4;

   x=2;

   input1;

   fprintf('input1 is %d', input1)

end

 

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

[A,B]=functionname(3,4)

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

input1=3;

input2=4;

It then carries out the code within the unction and then quits when it finds the function functionname when it finds the end statement.

To update x we would use the output name x

function [output1,output2]=functionname(input1,input2)

   output1=input1;

   output2=4;

   fprintf('input1 is %d', input1)

end

Typing the following into the Command Window:

[x,output2]=functionname(1,3)

 

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

function [output1,output2]=functionname(input1,input2)

   output1=input1;

   output2=4;

   fprintf('input1 is %d', input1)

end

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

[x,output2]=functionname([],3)

[x,output2]=functionname

 

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:

function [output1,output2]=functionname(input1,input2)

   output1=input1;

   output2=input2;

   fprintf('input1 is %d', input1)

end

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:

[x,y]=functionname(1,3)

 

 

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 inch2cm.

function [output_cm]=inch2cm(input_inch)

   % This simple function will convert the input in inches to the output in cm using the conversion factor 2.54.

   output_cm=2.54*input_inch;

end

 

 

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.

function [M]=randi2(min,max,m,n)

   % This will generate random integers between a negative minimum and positive maximum value

   M=randi(max-min+1,m,n)-min-1

end

 

Leave a Reply

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