Introduction to MatLab

This is a tutorial written for those who are non-technical but want to or need to learn to use Matlab. Now, don’t get worried about that word, program, it’s not like Matlab has pointers and memory allocation
that you have to worry about. In fact you won’t see those concepts on this site.

In this tutorial, I will use a few conventions. When you see lots of words in double quotes, that denotes what programmers call a string. Strings will be used for quotes, and other text that will relate to the Matlab code. I will also try to keep variable names in single quotes when I’m talking about them in the comments.

Functions will be represented as function name followed by open and close parentheses like this: functionName(). I will also use the Hungarian method for naming variables. The method goes as follows; the first letter of the variable is lower case, and all subsequent words start with an upper case. Here’s an example: oneLongDiscriptiveVariable. You don’t want to have variable names too long, for it’s a pain to type, but having it be too short is also bad, because later, you won’t remember what it was for.

So onto the show. Matlab which is short for Matrix Laboratory is a very powerful tool to do all sorts of calculations and plots. All of the variables are stored as matrices which makes matrix math really easy and fast. In my explanations, if my function description is unclear, you can use using Matlab’s truly wonderful built-in help. For example if you needed to know how bench() worked type:

help bench

Help can be used on any of the functions used in this tutorial.


How does this look like? Well, let’s make matlab store the following matrix:











in a variable called ‘A’. We can accomplish this by
issuing the command:

A = 1:1:10;

Now this is a loaded command, as lots of things are going on. The first 1 is the starting value of our series. The 1 in the middle is the increment, the separation between two consecutive values. The last number as you may have guessed is the final value. The trick in this command comes at the end, the ‘;’. This operator applies for all commands in Matlab, it suppresses any output to the console the function may normally give. For example our command, if there were no semi-colon would print out the matrix to the screen after assigning it to the variable ‘A’.

Variables can be arbitrarily named. You could call it ‘mom’, ‘homie’, ‘ourFirstVariable’, or even ‘HELLO’. The caveats to this are that if you have a variable named after a function, say ‘sin’, then later executions that would require accessing the trigonometric function sin() would in fact access elements within the variable ‘sin’. Another variable name that should be avoided is ‘ans’. This is because every result that isn’t placed in a variable, is automatically placed into the variable ‘ans’.


It is possible to create an M-File script which will allow for storing a sequence of commands. Under File->New it is possible to select M-File. The other options we will discuss later. Upon creating a new M-File a text-editor is opened up. You can type all the commands into this window, then you can execute them as many times as you wish by going under Debug->Run or pressing F5 on the keyboard.

Cell Mode

At this time, it’s worth to mention that to put in comments you can use the percent symbol, “%”. However, if you do “%%” it is the separator of these things called “cells”. What it allows you to do is have more control over the execution of your script. You can execute the cell that the cursor is in by pressing Ctrl+Enter. Try it by entering the following into an M-File:

%% Cell one
disp(“Cell 1”);
%% Cell two
disp(“Cell 2”);

I secretly introduced a new command here called disp(“String to print”). This function will print out the arguments given to it.


Say we wanted to plot our variable ‘A’. We could issue the command:


Now in this case, the ‘;’ is redundant, as the plot() function doesn’t output anything to the console. We can define attributes of the plot by running more functions. Like to set the title to “A simple line” we can do it by:

title('A simple line');

Notice that I used single quotes. That is Matlab’s way of representing strings. Confusing, I know, all I can say is “Get over it”. Moving on, to add labels, you would issue the xlabel(),ylabel(), and zlabel() functions.

Multiple plots

What happens when you want to generate multiple plots? Well, depends on whether you want to handle them one at a time, or all at once. Two commands are available to handle this. One is called pause which will halt execution of your Matlab program until the user presses a enter in the console. The other method is calling the figure(number) command before plotting. That way you can open up multiple plot windows each time you execute the figure(number) function.

If you’re averse to the idea of opening up a zillion plot windows, you can also use the subplot(rows,columns,selected) function. It, as the name states, creates many plots inside one plot window. Labeling your graphs can be rather tricky so I would probably advise against using it.

Okay, so explaining it is nice and all, but let’s see some examples.

A = 1:1:10;
B = 1:2:15;
figure(1); plot(A);
figure(2); plot(B);
figure(3); subplot(1,2,1); plot(A); subplot(1,2,2); plot(B);

So if you execute this code, you will notice that you need to press enter twice to get the first two plots Then immediately it will plot the two plots in individual windows. Pressing enter again makes a third plot window appear containing both plots.

So that’s my introduction to plotting and simple concepts that are good to know about.

Fekete András

Liked it? Take a second to support me on Patreon!

Leave a Reply