Matlab tutorial
<p>To take advantage of MATLAB's full capabilities, we need to know how to construct long (and sometimes complex) sequences of statements. This can be done by writing the commands in a file and calling it from within MATLAB. Such files are called "m-files" because they must have the filename extension "<code> .m</code>". This extension is required in order for these files to be interpreted by MATLAB.<!--more--></p>
<p>
There are two types of m-files: <code>script files</code> and <code>function files</code>. <strong>Script files</strong> contain a sequence of usual MATLAB commands, that are executed (in order) once the script is called within MATLAB. For example, if such a file has the name <code>compute.m</code> , then typing the command <code>compute </code>at the MATLAB prompt will cause the statements in that file to be executed. Script files can be very useful when entering data into a matrix.
</p>
<p>
<strong>Function files</strong>, on the other hand, play the role of user defined commands that often have input and output. You can create your own commands for specific problems this way, which will have the same status as other MATLAB commands. Let us give a simple example. The text below is saved in a file called <code>log3.m</code> and it is used to calculate the base 3 logarithm of a positive number. The text file can be created in a variety of ways, for example using the built-in MATLAB editor through the command <code>edit</code> (that is available with MATLAB 5.0 and above), or your favorite (external) text editor (e.g. <code>Notepad</code> or <code>Wordpad</code> in Microsoft Windows). You must make sure that the filename has the extension " <code>.m</code>" !
</p>
<pre name="code" class="js">
function [a] = log3(x)
% [a] = log3(x) - Calculates the base 3 logarithm of x.
a=log(abs(x))./log(3);
% End of function
end
</pre>
Using this function within MATLAB to compute log<sub>3</sub>(5), we get
<pre name="code" class="js">
>> log3(5)
ans =
1.4650
</pre>
Let us explain a few things related to the syntax of a function file. Every MATLAB function begins with a header, which consists of the following :
(a) the word <code>function</code>,
(b) the output(s) in brackets, (the variable a in the above example)
(c) the equal sign,
(d) the name of the function, which must match the function filename (<code>log3 </code>in the above example) and
(e) the input(s) (the variable <code>x</code> in the above example).
Any statement that appears after a "<code>%</code> " sign on a line is ignored by MATLAB and plays the role of comments in the subroutine. Comments are essential when writing long functions or programs, for clarity. In addition, the first set of comments after the header in a function serve as on-line help. For example, see what happens when we type
<pre name="code" class="js">
>> help log3
[a] = log3(x) - Calculates the base 3 logarithm of x.
</pre>
MATLAB gave us as "help" on the function we defined, the text that we included after the header in the file.
Finally, the algorithm used to calculate the base 3 logarithm of a given number, is based on the formula
<code>
log<sub>3</sub>(x)=ln(|x|)/ln(3).
</code>
Since the logarithm of a negative number is undefined, we use the absolute value for "safety". Also, note that we have allowed for a vector to be passed as input, by using element-wise division in the formula.
During a MATLAB session, we may call a function just like we did in the above example, provided the file is saved in the current (working) directory. This is the reason why in the beginning of this guide we suggested that you should create a working directory and switch to that directory from within MATLAB.
It should be noted that both types of m-files can reference other m-files, including themselves in a recursive way.
<strong>Catch up</strong>
<a href="http://learnbyexamples.org/matlab/matlab-tutorial-1-the-basic-features.html">Matlab Tutorial 1 – The basic features</a>
<a href="http://learnbyexamples.org/matlab/matlab-tutorial-2-vectors-and-matrices.html">Matlab Tutorial 2 – Vectors and matrices</a>
<a href="http://learnbyexamples.org/matlab/matlab-tutorial-3-built-in-functions.html">Matlab Tutorial 3 – Built-in functions</a>
<a href="http://learnbyexamples.org/matlab/matlab-tutorial-4-plotting.html">Matlab Tutorial 4 – Plotting</a>
Matlab tutorial