During this last tutorial we will focus on creating our own function. We will do this by utilizing a previously written script. Even if you will not be using the function, practice makes perfect. And the more you practive your programming skills, the easier it is to create your own scripts.
Learning Goals #
- Understand how to create your own function in MATLAB.
- Embed earlier functions into other MATLAB scripts.
1. Creating our own function #
% Variables
Tired = false;
current_time = 1500; % = 15:00
% If-statement
if Tired == true
disp("Get coffee");
elseif current_time < 1600
disp("Okaay, 1 more. Otherwise no sleep for you");
else
disp("No coffee for you");
end
1.2 Inputs and outputs of the function #
EXERCISE 1
- What would be the inputs when we use the script above and transform this into a functions?
- What would be the outputs?
After you’ve thought about it for a second, we can conclude that we would need to use the variables listed above as the inputs. An the output will be a piece of text telling you if you could have another coffee of not.
1.2 Creating the actual function #
The most basic form of a function is written in the code-box below. Remember, this function is going to be written in a seperate script-file.
function output = function_name(inputs...)
What will happen in this function
So now we want to change this model-function so that it will work for our coffee script. We need to change the output, function_name, inputs and the actual script. As discussed before, we are going to use the variables listed above as input, and the output will be some sort of text indicating if we would need another coffee. And the actual script is already written above. This would result in the following function:
function answer = coffee_time(Tired, current_time)
if Tired == true
answer = "Get coffee";
elseif current_time < 1600
answer = "Okaay, 1 more coffee. Otherwise no sleep for you";
else
answer = "No coffee for you";
end
If we look abit closer to this function, you can see that we changes the dip(“…”) to a new variable answer. This variable is the output, and can also be found in the beginning of the function.
1.3 Using a function #
We have now succesfully written our own function. To make it work we need to make sure it is saved under the same name as the function name. After you have done this, you can start using this function like any other function already included in MATLAB. Remeber: when making our own functions, we can call this function in another script as long as it is saved in the same folder.
EXERCISE 2
- Try and use this function in another scipt.
- Does this function behave as you would would expect from any other function?
- What happens when you type help coffee_time into the Command Window?
1.4 Annotating the function #
>> help coffee_time
coffee_time is a function.
answer = coffee_time(Tired, current_time)
In exercise 2 we looked at our own function. And in the last part of this exercise we looked at what happens when we ask for help in the Command Window. This will give only minor information. So we can add annotations to the function to make it more understandable for yourself and other people using this function. To do this we are going to add notes in a similar way as other functions. In the code-box below you can see how functions are generally annotated.
% coffee_time - returns if it is time for coffee
% This function is meant to check if you need another coffee
% Inputs:
% Tired: if you are tired, give "true", otherwise give "false"
% current_time: supply the current time in military time, 16:00 -> 1600
% Output:
% answer: gives a string containing if coffee is needed
function answer = coffee_time(Tired, current_time)
if Tired == true
answer = "Get coffee";
elseif current_time < 1600
answer = "Okaay, 1 more coffee. Otherwise no sleep for you";
else
answer = "No coffee for you";
end
If you now save this script again and type help coffee_time in the Command Window again, you will see that more information is now added.
>> help coffee_time
coffee_time - returns if it is time for coffee
This function is meant to check if you need another coffee
Inputs:
Tired: if you are tired, give "true", otherwise give "false"
current_time: supply the current time in military time, 16:00 -> 1600
Output:
answer: gives a string containing if coffee is needed
For more information also look at the MATLAB website. Here you can find even more information about how to work with everything from matrices to different functions, and so much more.
Concluding remarks #
We have made huge steps already in these first hours of using MATLAB. From here on, it will only get easier. Now that we know how to use the program, we can start exploring what other powerful tools exist in the toolkit. This, as well as learning about other concepts, will be topics of further tutorials.