Functions in MATLAB (Or Octave)#

Just like in Python, we can make functions and run them to break up our code. There are a few things to remember, especially when working with the expectations of the autograder for the course.

  1. Functions should be in their own file, that is named the same name as the function.

    • If you have a function called count, your file for the function should be named count.m

  2. Do not mix standalone executable code (a script) and a function definition in the same file.

    • This is mainly an Octave thing. MATLAB now pre-compiles scripts and functions prior to running them, so it can see fuctions later on in the file. Octave cannot.

    • A file must be either a script OR a function file. While MATLAB is forgiving, Octave requires a file to start with the function definition if it is a function file IN LINE 1. Do not add additional comments or anything prior to the function header.

  3. You can define multiple functions in one file, but it must start with the main, or primary, function. Only this primary function can be called from the command line or another file.

    • Any subsequent functions in the file are called subfunctions (or helper functions) and are private, meaning they can only be called by the primary function or other subfunctions within that same file.

Function Syntax#

The syntax to make a function in MATLAB/Octave is:

function [output1, output2, ...] = functionName(input1, input2, ...)

This line must be in LINE 1 of the file, and the file name should be same as the function name. The function will be called just like a function in python where a script (or other function) can call [out1,out2] = functionName(a,b,c,...) and then the variables out1 and out2 can be used in different parts of the code.

Inside the function, you do not need to use return or anything like that, just make sure you are defining your output variables inside of the function. Once the function reaches the end, it sends the variables listed in the function header to the output of the function.

You can also call a null function, meaning it doesn’t return any values by setting up a function like:

function functionName(input1, input2, ...)

Make sure you run your functions before calling them!#

MATLAB and Octave are scripting languages, meaning they basically just run the commands from a script or function in order in a command line. If you plan on having some variables ready, or some functions that are not directly saved in a file, make sure they are initialized (sent into the command line) prior to using them.

In order for this book to work, run the following code in order, otherwise you will get ‘undefined function’ errors. (Or feel free to refresh the page and run them in different orders to see what happens)

NOTE: All of the MATLAB interactive pages are going to look a little weird, I have no idea why. There is a bunch of junk whitespace that keeps getting made when it compiled for the website, and I have no idea where it is coming from. Just press the rocket, and then run the code a couple of times, and it will clear up.

function output = equation1(x,y,z)

    output = 2.5*x + 8*y + 4*z;

end 
?2004l


?2004h
?2004l


warning: using the gnuplot graphics toolkit is discouraged

The gnuplot graphics toolkit is not actively maintained and has a number
of limitations that are unlikely to be fixed.  Communication with gnuplot
uses a one-directional pipe and limited information is passed back to the
Octave interpreter so most changes made interactively in the plot window
will not be reflected in the graphics properties managed by Octave.  For
example, if the plot window is closed with a mouse click, Octave will not
be notified and will not update its internal list of open figure windows.
The qt toolkit is recommended instead.
?2004h
?2004l


?2004h
?2004l


?2004h
?2004l

?2004h
?2004l

?2004h
?2004l

?2004h
?2004l

?2004h
function [value_1,value_2,value_3] = equation2(q,r,s)

    value_1 = q * r;
    value_2 = (r^2) + 2*s;
    value_3 = s - (0.5 * q);

end 
?2004l


?2004h
?2004l

?2004h
?2004l

?2004h
?2004l


?2004h
?2004l


?2004h
?2004l

?2004h
?2004l

?2004h
a = 3;
b = 9;
c = 4;

equation_result = equation1(a,b,c);
disp(equation_result)


[result_1, result_2, result_3] = equation2(a,b,c);

disp(result_1)

disp(result_2)

disp(result_3)
?2004l

?2004h
?2004l

?2004h
?2004l

?2004h
?2004l

?2004h
?2004l


?2004h
?2004l


95.500
?2004h
?2004l

?2004h
?2004l

?2004h
?2004l


?2004h
?2004l

?2004h
?2004l


27
?2004h
?2004l

?2004h
?2004l


89
?2004h
?2004l

?2004h
?2004l


2.5000
?2004h