I recently talked about how to use the Matlab command line as well as the editor. We have learned how to do some simple calculations and fill the results in matrices. We also saw that using the semi-colon operator can turn on or off the display of the results on the command line.

Number are nice and precise but our brain is not very good at remembering them. Except for a few rare individuals, we are much better on images.

Don’t worry, the point of this post is not to talk about Neurosciences (well, I would love to but this is matlabtips.com) but about how to display data in a more convenient way in Matlab. When it comes to this, Matlab is quite handy.

Let’s suppose you have two 1D matrices: X and Y and you want to display them on a plot. The first thing to do is to create your figure. Matlab needs a physical window where to place your plot. This is done using the function figure.

So the code would look like :

X=1:100;
Y=X+10;
figure;
plot(X,Y);

In this code, line 1 and 2 are just there to create X and Y, figure create the window and plot is the actual function that create your plot in the window. You should get something like this :

You probably notice at this point that this figure has a lot of nice buttons you can interact with. I will let you interact with them. These are quite intuitive.

One important rule is that X and Y needs to be of the same size, otherwise Matlab does not know where to position its points.

If you run this code several times, I am sure you will notice that a new window is created each time. This can become annoying. So you might want to tell Matlab to always use the same window. This can be done by giving a fix number or handle to your figure with the following code :

X=1:100;
Y=X+10;
figure(1);
plot(X,Y);

Now your plot is always on **figure number 1**. This is a nice way to create several figures and makes sure that things stay organize on your desktop.

I guess now you are obviously wondering how to label properly your graph so that it can be communicated to others easily. To do this, you can use

xlabel,

ylabel and

title. You need to pass a string to these functions, that is why I will use the character ‘ in the following code.

X=1:100;
Y=X+10;
figure(1);
plot(X,Y);
title('This is your title');
xlabel('label of the X axis');
ylabel('label of the Y axis');

Now it is much more self-informative, isn’t it?

There are a number of plots that you can use : bar, loglog, plotyy, … I encourage you to explore.

*Related*

Wow, so much interesting stuff and so nicely written (some of the English would need some polishing, but I can’t really comment on that as I am not a native speaker myself).

I have worked through the basics in a somewhat confusing 4-weeks-training in my university and come here to strengthen the basic concepts as well as trying to go further (future plans involve drone simulations and sensor applications, but that needs more practice).

I really appreciate the work you put into this – you should consider publishing a printed version, I would certainly buy it.