64

Making Custom Matlab Functions


Article posted on Monday, 12th, August, 2013 at 10:01 pm


Matlab is full of functions specifically designed to make your life easier. You should have an idea of how to use them, since we’ve already used a few in our examples. Making your own functions is a great way of saving yourself time for doing tasks you know you’ll need to do in the future…

Function basics

To use a function, simply call it using its name, and then pass in any arguments to that function using the parentheses (). In its simplest form, a function will look like this:

function_name()

This assumes that the function doesn’t require any input for it to run. This function could do something like return the current date and time, formatted in a way that is useful to you, or maybe it just uses disp to give you an inspirational message!

An example of a simple static function would be:

function say_hello()

  disp('Hello!')

end  

REALLY simple. Now whenever you call say_hello(), you’ll get ‘Hello!’ printed to the command line. Note that you don’t need to use the end keyword to end the function, but it helps if you have more than one function in the same Matlab script.

Anyway, this is a bit basic, so you’re probably going to want to do something that requires input variables.

Function Input Arguments

No, I’ve not fallen out with the function, the variables you pass to it are called argumets. Examples would be a path to a file you need to read and process, a user ID you want to get the info of, or a matrix you want to apply some operations to. For simplicity’s sake, lets use my age example from the if statements post:

function check_age(age)

  if age >= 18
    disp('You can buy beer!')
  else
    disp('No beer for you!')
  end
  
end  

Now you can specify as many input variables as you want, simply by adding them to the function declaration, separated by the comma. You don’t need to use them in your function for it to work, but if you specify them, you MUST provide them as inputs to your function. In this case, we now have a variable to specify the age limit to buy beer – for countries with laws different to the UK.

function check_age(age, age_limit)

  if age >= age_limit
    disp('You can buy beer!')
  else
    disp('No beer for you!')
  end
  
end  

If you want your program to recieve an unspecified number of inputs, you can use varargin, but I’ll save that for another time.

Function Output Variables

For a half decent function that you can reuse, it’s probably a good idea that you make it so that you pass some inputs, and the function gives you back some outputs. This is done using the assignment operator in the function declaration (=):

function can_buy = check_age(age, age_limit)

  if age >= age_limit
    can_buy = 1;
  else
    can_buy = 0;
  end
  
end  

Now the variable can_buy is returned upon the successful execution of the function check_age. Note that can_buy will be either 1 or 0. This means we can use it in an if statement to avoid repeating code in future:

if check_age(age, age_limit)
  disp('You can buy beer!')
else
  disp('No beer for you!')
end

In this rather poor example, we have made our code longer and less efficient, by replacing the logical operator age >= 18, with the function check_age(age, age_limit), but hopefully you get the idea how this can actually be useful.

If your function is really fancy, you can receive several variables back from it! To catch them all, you need to assign each one to an array of variables. Each variable will have the corresponding data in it when the function has executed.

function [can_buy, age_diff] = check_age(age, age_limit)

  age_diff = age - age_limit;

  if age >= age_limit
    can_buy = 1;
  else
    can_buy = 0;
  end
  
end  

So now you should notice that our function returns a boolean value denoting whether our specified age can buy beer, and also the number of years differnce between the specified age, and the age limit. We can then use that to tell under age users how long they have to wait until they can buy their precious beer…

if check_age(age, age_limit)
  disp('You can buy beer!')
else
  disp(['No beer for you! Come back in ', num2str(abs(age_diff)), ' years.'])
end

Note we’re using the abs function, which returns the absolute (i.e. non-negative) value of a number. In our case, age_diff would have been a negative number (age minus age_limit) where a user was too young to buy beer.

Hopefully that was fairly straightforward. Now go out and use it on something more appropriate!



Tags: , , , , , ,

Join in the Conversation


comments powered by Disqus