Plotting 2D Figures in Octave/MATLAB

Introduction

This will take you through the basics of performing a 2D plot in Octave, taking data in from an Excel Spreadsheet.

Figure

The function:

<span style="color: #3366ff;">figure</span>

Will create a new figure taking the lowest unused number of figure.

If an input argument is selected for instance

<span style="color: #3366ff;">figure(</span>100<span style="color: #3366ff;">)</span>

And that figure does not exist, it will create it:

Otherwise it can be used to select a figure, for instance if we wanted to select "Figure 1" again we would type in

<span style="color: #3366ff;">figure(</span>1<span style="color: #3366ff;">)</span>

This is extremely useful but one should be careful that they don't overwrite a figure by accident when using it.

Data

We need some data to plot. We can create this data in Octave/MATLAB or input it from a spreadsheet in the likes of Excel. We are going to copy data from excel using the variable editor (the copy paste method described here will only work for data that contains numbers, if an excel cell that is copied contains a letter it will not work). In MATLAB we can directly right click the variable editor and create a new variable. In Octave we need to first make a variable and assign it to a scalar before it allows to copy and paste excel data into the variable editor. So for instance using variable names which match the tab in the excel spreadsheet we can type in the following gives a rawdata a scalar of 1:

rawdata=1

We can double click

rawdata

in the workspace to open up in the variable editor. Here we see a spreadsheet with a single scalar value of 1 as expected. Now we can right click and select paste table which will paste the Excel data:

The excel data is pasted across:

We can then select the other tab of the excel data sheet interdata which is interpolated data calculated from the rawdata. First we will need to create the new variable and assign in to a scalar

interdata=1

Then we can open up interdata in the variable editor by double clicking it in the workspace. You may want to press the x to close rawdata in the variable editor (it will remain in the workspace) to give the most screen space:

Once again we can copy and paste across from excel by right clicking and using Paste Table:

The data we want to plot is now in Octave:

The data I am going to use can also be generated using the script here:

[code language="matlab"]

% Data
rawdata=[0,0;
10,227.04;
15,362.78;
20,517.35;
22.5,602.97;
30,901.67];

% Lower and Upper Limits for interpolation
m=1;
n=30;

% Interpolation Spreadsheet
dataint(:,1)=[m:n]';
dataint(:,2)=interp1(rawdata(:,1),rawdata(:,2),dataint(:,1),'Nearest');
dataint(:,3)=interp1(rawdata(:,1),rawdata(:,2),dataint(:,1),'Linear');
dataint(:,4)=interp1(rawdata(:,1),rawdata(:,2),dataint(:,1),'Pchip');

[/code]

More details about it are available in my guide on Interpolation.

Column Selection

In order to have data to plot, we need to be able to select columns from the variable. I go in quite some details about how to do this in my guide Introduction to Arrays however here what we need to know is that for a variable called

variable

, in order to select the first column we need to type in

<span style="color: #3366ff;">variable(</span><span style="color: #ff0000;">:</span>,<span style="color: #ff0000;">1</span><span style="color: #3366ff;">)</span>

. Here

<span style="color: #ff0000;">:</span>

denotes all rows and

1

denotes the first column and the input arguments are enclosed in round brackets and separated with a comma. To select the second column we type in 

<span style="color: #3366ff;">variable(</span><span style="color: #ff0000;">:</span>,<span style="color: #ff0000;">2</span><span style="color: #3366ff;">)</span>

.

Plot

The function

<span style="color: #3366ff;">plot</span>

is a powerful function and takes in multiple input arguments. The first two input arguments should be the x and y data you want to plot.

<span style="color: #3366ff;">plot(</span>x,y<span style="color: #3366ff;">)</span>

Here we want the

x

data to be the first column of

rawdata

and the

y

data to be the second column of

rawdata

. Therefore we can assign

x=<span style="color: #3366ff;">rawdata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #3366ff;">)</span>
y=<span style="color: #3366ff;">rawdata(</span>:,<span style="color: #ff0000;">2</span><span style="color: #3366ff;">)</span>

We can then create a new figure by typing in

<span style="color: #3366ff;">figure</span>

Now we can type in

<span style="color: #3366ff;">plot(</span>x,y<span style="color: #3366ff;">)</span>

The figure here is very basic. Notice how the blue line is narrow and the text is quite tiny. There is also no axis label or legend. All of these can be tweaked with additional lines of code. Let us open up the Editor tab and create a new script and paste the last line into it:

We can then press the save button, once we do this a dialogue box shows and we can input a script name and select save. More details about variable names are in my guide Introduction to Arrays.

Line Width

Let's us first thicken the line so we can see it in more detail, to do this we need to add the additional input argument(s)

<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span>

and

5

.

<span style="color: #3366ff;">plot(</span>x,y,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5</span></span><span style="color: #3366ff;">)</span>

Notice how we type in

<span style="color: #800080;">' '</span>

around the word LineWidth and it changes colour

<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span>

 this is because LineWidth unenclosed would be taken by Octave/MATLAB as a variable or function. Instead we want this input argument to be a line of text, known as a string. These input arguments are matched so we must immediately specify the line width we wish to use, in this case we are using the scalar 5. In general these functions are setup so the case of the string doesn't matter

<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span>

 or 

<span style="color: #800080;">'</span><span style="color: #ff9900;">linewidth</span><span style="color: #800080;">'</span>

 should work equally well.

Note: The colours shown in the script editor are different to the ones I am using in these notes, because I am using colours to highlight some things.

When editing code in the script editor, be sure to save it before you run it by pressing the save button. Unsaved code will have a * at the beginning of the title as shown. Once saved right click the script and select run:

Now the Line Width is updated to a thickness of 5:

Font Size

Another thing that is hard to see is the text. To update the size of this we need to use the function

<span style="color: #3366ff;">set(</span>gca,<span style="color: #800080;">'</span><span style="color: #ff9900;">FontSize</span><span style="color: #800080;">'</span>,20<span style="color: #3366ff;">)</span>

We need to use this function set with 3 input arguments

gca

stands for get current axis meaning you are changing the properties of the axis. As we want to change the Font Size we need to use the word

<span style="color: #800080;">'</span><span style="color: #ff9900;">FontSize</span><span style="color: #800080;">'</span>

and accompany it with the desired Font Size in this case 20. Save and rerunning the script we get the axes with the increased font size.

Line Colour

To change the colour of the line we can add two additional input arguments:

<span style="color: #3366ff;">plot(</span>x,y,<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Red</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5</span></span><span style="color: #3366ff;">)</span>

Here we need to be careful because Octave/MATLAB use American English and not British English, so we need to spell colour as color. This input argument needs to be followed by the colour. As you can see the line is now red opposed to blue.

In the code above

<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>

,

<span style="color: #800080;">'</span><span style="color: #ff9900;">Red</span><span style="color: #800080;">'</span>

,

<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span>

and

<span style="color: #800080;">'</span><span style="color: #ff9900;">FontSize</span><span style="color: #800080;">'</span>

are all strings and must be enclosed by

<span style="color: #800080;">' '</span>

 . The abbreviated string

<span style="color: #993366;">'</span><span style="color: #ff9900;">r</span><span style="color: #993366;">'</span>

can be used in the place of

<span style="color: #993366;">'</span><span style="color: #ff9900;">Red</span><span style="color: #993366;">'</span>

. However all colours are made from a mixture of three primary colours – red, green and blue. A colour can be expressed as a

[<span style="color: #ff0000;">r</span>,<span style="color: #00ff00;">g</span>,<span style="color: #0000ff;">b</span>]

row vector opposed to a string.

MATLAB/Octave have the following strings for the primary and secondary colours:

\displaystyle \begin{array}{*{20}{c}} {\text{Specifier Long}} & {\text{Specifier}} & {\text{RGB Row Vector}} \\ {\text{black}} & \text{k} & {\left[ {0,0,0} \right]} \\ {\text{red}} & \text{r} & {\left[ {1,0,0} \right]} \\ {\text{green}} & \text{g} & {\left[ {0,1,0} \right]} \\ {\text{blue}} & \text{b} & {\left[ {0,0,1} \right]} \\ {\text{yellow}} & \text{y} & {\left[ {1,1,0} \right]} \\ {\text{magenta}} & \text{m} & {\left[ {1,0,1} \right]} \\ {\text{cyan}} & \text{c} & {\left[ {0,1,1} \right]} \\ {\text{white}} & \text{w} & {\left[ {1,1,1} \right]} \end{array}

Black is shown in the absence of these three colours and white is shown when these three colours are at full brightness.

Many programs list colors in 8 bit, that means they list \displaystyle {{2}^{8}}=256 levels. If we subtract 1 from 256 to get a 0 value with range from 0-255. You may use colour pickers in these programs to determine the colour you want to use. Let's use the colour picker in Microsoft Word for example. Select the button to change the colour of the text, select More Colours:

Select your colour and then select custom:

You now get your RGB values listed from 0 to 255. We can normalise these to 255 but dividing them through by 255 when using them in Octave/MATLAB:

Updating the code to plot the line in this colour

<span style="color: #3366ff;">plot(</span>x,y,<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,[<span style="color: #ff0000;">51/255</span>,<span style="color: #00ff00;">51/255</span>,<span style="color: #0000ff;">255/255</span>],<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5</span></span><span style="color: #3366ff;">)</span>

Other programs like Microsoft Paint also have colour pickers, you only need the

[<span style="color: #ff0000;">r</span>,<span style="color: #00ff00;">g</span>,<span style="color: #0000ff;">b</span>]

row vector values and can ignore the Hue, Sat and Lum:

Line Style

Octave/MATLAB also has a number of different types of lines, by default the line is a solid line however another two input arguments can be input to change these:

\displaystyle \begin{array}{*{20}{c}} {\text{Line Style}} & {\text{Specifier}} \\ {\text{solid line}} & \text{-} \\ {\text{dashed line}} & {\text{--}} \\ {\text{dotted line}} & \text{:} \\ {\text{dash-dot line}} & {\text{- }\text{.}} \end{array}

Once again

<span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>

and

<span style="color: #800080;">'</span><span style="color: #ff9900;">--</span><span style="color: #800080;">'</span>

are both strings and must be enclosed in

<span style="color: #800080;">'</span>  <span style="color: #800080;">'</span>
<span style="color: #3366ff;">plot(</span>x,y,<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,[<span style="color: #ff0000;">51/255</span>,<span style="color: #00ff00;">51/255</span>,<span style="color: #0000ff;">255/255</span>], <span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">--</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5</span></span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">plot(</span>x,y,<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,[<span style="color: #ff0000;">51/255</span>,<span style="color: #00ff00;">51/255</span>,<span style="color: #0000ff;">255/255</span>], <span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #800080;"><span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5</span></span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">plot(</span>x,y,<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,[<span style="color: #ff0000;">51/255</span>,<span style="color: #00ff00;">51/255</span>,<span style="color: #0000ff;">255/255</span>], <span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #800080;"><span style="color: #ff9900;">-.</span></span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5</span></span><span style="color: #3366ff;">)</span>

Marker

Markers can also be added to the line using two more additional input arguments:

<span style="color: #3366ff;">plot(</span>x,y,<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,[<span style="color: #ff0000;">51/255</span>,<span style="color: #00ff00;">51/255</span>,<span style="color: #0000ff;">255/255</span>], <span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #800080;"><span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5,<span style="color: #993366;">'</span><span style="color: #ff9900;">Marker</span><span style="color: #993366;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">s</span><span style="color: #993366;">'</span></span></span><span style="color: #3366ff;">)</span>

Once again

<span style="color: #800080;">'</span><span style="color: #ff9900;">Marker</span><span style="color: #800080;">'</span>

and

<span style="color: #800080;">'</span><span style="color: #ff9900;">s</span><span style="color: #800080;">'</span>

are both strings and must be enclosed in

<span style="color: #800080;">'</span>  <span style="color: #800080;">'</span>

\displaystyle \begin{array}{*{20}{c}} {\text{Specifier}} & {\text{Specifier Long}} & {\text{Description}} \\ \text{+} & {} & {\text{plus signs}} \\ \text{o} & {} & {\text{circles}} \\ \text{*} & {} & {\text{asterisk}} \\ \text{.} & {} & {\text{point}} \\ \text{x} & {} & {\text{cross}} \\ \text{s} & {\text{square}} & {\text{square}} \\ \text{d} & {\text{diamond}} & {\text{diamond}} \\ {\text{ }\!\!\hat{\ }\!\!\text{ v }<\text{ }>} & {} & {\text{triangles varying orientation}} \\ \text{p} & {\text{pentagram}} & {\text{pentagram}} \\ \text{h} & {\text{hexagram}} & {\text{hexagram}} \end{array}

To see this in the script editor I have split the code on separate lines by pressing enter after the commas, input arguments are shown in pairs. The code will still run like this.

Marker Size

The markers above were quite small, they are hard to read, this can be resolved by using another two additional input arguments:

<span style="color: #3366ff;">plot(</span>x,y,<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,[<span style="color: #ff0000;">51/255</span>,<span style="color: #00ff00;">51/255</span>,<span style="color: #0000ff;">255/255</span>], <span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #800080;"><span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5,<span style="color: #993366;">'</span><span style="color: #ff9900;">Marker</span><span style="color: #993366;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">s</span><span style="color: #993366;">'<span style="color: #000000;">,</span>'<span style="color: #ff9900;">MarkerSize</span>'<span style="color: #000000;">,20</span></span></span></span><span style="color: #3366ff;">)</span>

Once again

<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerSize</span><span style="color: #800080;">'</span>

 is a string and must be enclosed in

<span style="color: #800080;">'</span>  <span style="color: #800080;">'</span>

 and 20 is a scalar.

Marker Edge and Face

The Marker does not need to be the same colour as the line. We can specify both the Edge and the Fill of the marker with another colour using another four input arguments. Once again

<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFace</span><span style="color: #800080;">'</span>

and

<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdge</span><span style="color: #800080;">'</span>

are both strings and must be enclosed in

<span style="color: #800080;">'</span>  <span style="color: #800080;">'</span>

. Each of them has a paired input argument, which uses the same notation as the line color (see above), I will use the 

[<span style="color: #ff0000;">r</span>,<span style="color: #00ff00;">g</span>,<span style="color: #0000ff;">b</span>]

row vector notation.

<span style="color: #3366ff;">plot(</span>x,y,<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,[<span style="color: #ff0000;">51/255</span>,<span style="color: #00ff00;">51/255</span>,<span style="color: #0000ff;">255/255</span>], <span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #800080;"><span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5,<span style="color: #993366;">'</span><span style="color: #ff9900;">Marker</span><span style="color: #993366;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">s</span><span style="color: #993366;">','<span style="color: #ff9900;">MarkerSize</span>'<span style="color: #000000;">,20,'<span style="color: #ff9900;">MarkerEdge</span><span style="color: #800080;">'</span> ,[</span><span style="color: #ff0000;">0/255</span><span style="color: #000000;">,</span><span style="color: #00ff00;">176/255</span><span style="color: #000000;">,</span><span style="color: #0000ff;">80/255</span><span style="color: #000000;">],'<span style="color: #ff9900;">MarkerFace</span>' ,[<span style="color: #ff0000;">255/255</span>,<span style="color: #00ff00;">0/255</span>,<span style="color: #0000ff;">0/255</span>]</span></span></span></span><span style="color: #3366ff;">)</span>

Short Hand

For learning pairing the inputs may help for using the plot function but it is quite useful to use the abbreviated notation.

<span style="color: #3366ff;">plot(</span>x,y, <span style="color: #800080;">'</span><span style="color: #ff0000;">color</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #00ffff;">b</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff0000;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #00ffff;">:</span><span style="color: #800080;">'</span>,'<span style="color: #ff0000;">Marker</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #00ffff;">s</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span>,5,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerSize</span><span style="color: #800080;">'</span>,20,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFace</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">g</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdge</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">r</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>x,y,<span style="color: #800080;">'</span><span style="color: #00ffff;">b:s</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span>,5,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerSize</span><span style="color: #800080;">'</span>,20,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFace</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">g</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdge</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">r</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

Scatter using Plot

There is a dedicated separate function

<span style="color: #3366ff;">scatter</span>

which works similarly to

<span style="color: #3366ff;">plot</span>

 and has some slight advantages but has slightly different input arguments, I'm not looking to cover it here as one can also make a scatter plot by using the short hand notation for plot, the long hand notation does not work because it will default to plot a line if the input argument 

<span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>

 is absent:

<span style="color: #3366ff;">plot(</span>x,y,<span style="color: #800080;">'</span><span style="color: #00ffff;">bs</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerSize</span><span style="color: #800080;">'</span>,20,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFace</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">g</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdge</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">r</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

Grid Lines

One can enable the grid by using the command

<span style="color: #3366ff;">grid(<span style="color: #ffcc00;">'on'</span>)</span>
<span style="color: #3366ff;">grid on</span>

And also the minor grid lines by using the command

<span style="color: #3366ff;">grid(<span style="color: #ffcc00;">'minor'</span>)</span>
<span style="color: #3366ff;">grid minor</span>

Axis Limits

When we changed the size of the font, we used the function

<span style="color: #3366ff;">set(</span>gca,<span style="color: #800080;">'</span><span style="color: #ff9900;">FontSize</span><span style="color: #800080;">'</span>,20<span style="color: #3366ff;">)</span>

With 

gca

standing for get current axis meaning you are changing the properties of the axis.  To change the axis limits we use the same function with different input arguments or alternatively we can combine the input arguments and put them on one line.

This time the input argument is 

<span style="color: #800080;">'</span><span style="color: #ff9900;">XLim</span><span style="color: #800080;">'</span>

 an abbreviation for x limits and is a string so must be enclosed in

<span style="color: #800080;">'</span>  <span style="color: #800080;">'</span>

. It's matched input argument is a 2 by 1 row vector consisting of the upper and lower x limits respectively.

<span style="color: #3366ff;">set(</span>gca,<span style="color: #800080;">'</span><span style="color: #ff9900;">XLim</span><span style="color: #800080;">'</span>,[xlower,xupper]<span style="color: #3366ff;">)</span>

Likewise we have the input argument is 

<span style="color: #800080;">'</span><span style="color: #ff9900;">YLim</span><span style="color: #800080;">'</span>

 an abbreviation for y limits and is likewise a string so must be enclosed in

<span style="color: #800080;">'</span>  <span style="color: #800080;">'</span>

. It's matched input argument is also a 2 by 1 row vector this time consisting of the upper and lower y limits respectively.

<span style="color: #3366ff;">set(</span>gca,<span style="color: #800080;">'<span style="color: #ff9900;">Y</span></span><span style="color: #ff9900;">Lim</span><span style="color: #800080;">'</span>,[ylower,yupper]<span style="color: #3366ff;">)</span>

These can be combined so

<span style="color: #3366ff;">set(</span>gca,<span style="color: #800080;">'</span><span style="color: #ff9900;">XLim</span><span style="color: #800080;">'</span>,[xlower,xupper],<span style="color: #800080;">'</span><span style="color: #ff9900;">YLim</span><span style="color: #800080;">'</span>,[ylower,yupper]<span style="color: #3366ff;">)</span>

For instance if we want to change the limits for

xlower=0

,

xupper=12

,

ylower=0

and

yupper=300
<span style="color: #3366ff;">set(</span>gca,<span style="color: #800080;">'</span><span style="color: #ff9900;">XLim</span><span style="color: #800080;">'</span>,[0,12],<span style="color: #800080;">'</span><span style="color: #ff9900;">YLim</span><span style="color: #800080;">'</span>,[0,300]<span style="color: #3366ff;">)</span>

Axis Scale

We can also set the axes scale to log using

<span style="color: #3366ff;">set(</span>gca,<span style="color: #800080;">'<span style="color: #ff9900;">X</span></span><span style="color: #ff9900;">Scale</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Log</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">set(</span>gca,<span style="color: #800080;">'<span style="color: #ff9900;">Y</span></span><span style="color: #ff9900;">Scale</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Log</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">Log</span><span style="color: #800080;">'</span>

is a string so must be enclosed in

<span style="color: #800080;">'</span>  <span style="color: #800080;">'</span>

.

In this case only changing the y axis to log

Axis Labels and Title

There are three functions to label the x-axis, y-axis and the figure title these are

<span style="color: #3366ff;">xlabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">your x label</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">ylabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">your y label</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">title(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">your title</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

In my case, the chart labels are

<span style="color: #3366ff;">xlabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">time (s)</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">ylabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">speed (m/s)</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">title(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">speed of a rocket with respect to time</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

Note how the brackets enclosed within the 

<span style="color: #800080;">'</span>  <span style="color: #800080;">'</span>

 are treated as text and not as part of the code.

Quite often one may have created a cell array with the labels, these can also be input as axis labels. To create a cell array, one needs to use curly brackets. We can create for instance

<span style="color: #800080;"><span style="color: #ff0000;"><span style="color: #000000;">mytestlabels=</span>{</span>'<span style="color: #ff9900;">test</span> </span><span style="color: #ff9900;">time (s)</span><span style="color: #800080;">'<span style="color: #000000;">,</span>'<span style="color: #ff9900;">test</span> <span style="color: #ff9900;">speed (m/s)</span>'<span style="color: #000000;">,'<span style="color: #ff9900;">test</span> <span style="color: #ff9900;">speed of a rocket with respect to time</span>'<span style="color: #ff0000;">}</span></span></span>
<span style="color: #3366ff;">xlabel(<span style="color: #33cccc;">mytestlabels(<span style="color: #000000;">1</span>)</span>)</span>
<span style="color: #3366ff;">ylabel(<span style="color: #00ccff;">mytestlabels(<span style="color: #000000;">2</span>)</span>)</span>
<span style="color: #3366ff;">title(<span style="color: #00ccff;">mytestlabels(<span style="color: #000000;">3</span>)</span>)</span>

Legend

The graph is missing a legend we can add one using the following function

<span style="color: #3366ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">label1</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">NorthWest</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

For the location we use the compass points, these are usually within the chart area but we can also specify for them to be outside. Best is leaving MATLAB/Octave to find the best location:

\displaystyle \begin{array}{*{20}{c}} {\text{Location Inside}} & {\text{Location Outside}} \\ {\text{North}} & {\text{NorthOutside}} \\ {\text{NorthEast}} & {} \\ {\text{East}} & {\text{EastOutside}} \\ {\text{SouthEast}} & {} \\ {\text{South}} & {\text{SouthOutside}} \\ {\text{SouthWest}} & {} \\ {\text{West}} & {\text{WestOutside}} \\ {\text{NorthWest}} & {} \\ {\text{Best}} & {\text{BestOutside}} \end{array}

I will use

<span style="color: #3366ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">rawdata</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">NorthWest</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

Now for comparison I will use

<span style="color: #3366ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">rawdata</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">SouthOutside</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

Plotting Multiple Line Plots in a Single Figure

For this next part, I want the raw data to be plotted using red dots and no line.

[code language="matlab"]
figure
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines&amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;pre&amp;amp;amp;amp;gt;[/code]


To this plot, we want to add a straight dotted line of the nearest point interpolated data from the variable

<span style="color: #3366ff;">dataint</span>

(x data is

<span style="color: #3366ff;">dataint(</span>:,<span style="color: #ff0000;">1</span><span style="color: #3366ff;">)</span>

and y data is

<span style="color: #3366ff;">dataint(</span>:,<span style="color: #ff0000;">2</span><span style="color: #3366ff;">)</span>

) which is size 5 and is dotted. Therefore we can add the following line of code

<span style="color: #3366ff;">plot(<span style="color: #00ccff;">dataint(</span><span style="color: #000000;">:,</span><span style="color: #ff0000;">1</span><span style="color: #00ccff;">)</span></span>,<span style="color: #00ccff;">dataint(</span>:,<span style="color: #ff0000;">2</span><span style="color: #00ccff;">)</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">b</span><span style="color: #993366;">'</span>, <span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #800080;"><span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5</span></span><span style="color: #3366ff;">)</span>

Since the code starts with

figure

Octave/MATLAB will create a new figure, in the case the lowest unused figure is Figure 2. To Figure 2, Octave plotted the rawdata with red markers but then it seen a new plot command and replotted the dataint as a blue dotted line removing the former plot of raw data with red markers.

To prevent if from doing this we use the commands

<span style="color: #3366ff;">hold(<span style="color: #ffcc00;">'on'</span>)</span>

Which also works in the form:

<span style="color: #ff0000;">hold</span> <span style="color: #3366ff;">on</span>
<span style="color: #3366ff;">plot(<span style="color: #00ccff;">dataint(</span><span style="color: #000000;">:,</span><span style="color: #ff0000;">1</span><span style="color: #00ccff;">)</span></span>,<span style="color: #00ccff;">dataint(</span>:,<span style="color: #ff0000;">2</span><span style="color: #00ccff;">)</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">b</span><span style="color: #993366;">'</span>, <span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #800080;"><span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5</span></span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">hold(<span style="color: #ffcc00;">'off'</span>)</span>
<span style="color: #ff0000;">hold</span> <span style="color: #3366ff;">off</span>

The

<span style="color: #ff0000;">hold</span> <span style="color: #3366ff;">on</span>

command holds the current plot, while this is active, any other

<span style="color: #3366ff;">plot</span>

command will add to the existing figure, opposed to overwriting the previous figure. The 

<span style="color: #ff0000;">hold</span> <span style="color: #3366ff;">off</span>

command should be used when finished adding all the data you want to the current figure and then if you want to create a separate figure you should use the command

<span style="color: #3366ff;">figure</span>

afterwards.

Closing figure 1 and figure 2 and relaunching the updated script:

Now we can see how the nearest interpolation looks with respect to the original data. Since the plot now has two items, a combined legend can be made:

<span style="color: #3366ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">rawdata</span><span style="color: #800080;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">nearest point interpolation</span><span style="color: #993366;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">NorthWest</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

The code is like

[code language="matlab"]
figure
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,2),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','nearest point interpolation','Location','NorthWest')
[/code]

In separate figures I also want the linear interpolated data and cubic interpolated data. These are the 3rd and 4th column of dataint respectively. To do this I can copy and paste the above code 3 times and update line 17 to use the 3rd and 4th column instead of the second and line 20 to update the legend labels accordingly.

[code language="matlab"]

figure
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,2),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','nearest point interpolation','Location','NorthWest')
figure
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,3),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','linear interpolation','Location','NorthWest')
figure
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,4),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','cubic interpolation','Location','NorthWest')

[/code]

Subplots

We have three separate figures, it is possible to combine them together using a subplot. Subplotting essentially splits the figure window up into a m by n matrix

For example in the case above where m=2 and n=2

<span style="color: #3366ff;">figure</span>

<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">2</span>,<span style="color: #00ccff;">2</span>,<span style="color: #800080;">1</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">2</span>,<span style="color: #00ccff;">2</span>,<span style="color: #800080;">2</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">2</span>,<span style="color: #00ccff;">2</span>,<span style="color: #800080;">3</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">2</span>,<span style="color: #00ccff;">2</span>,<span style="color: #800080;">4</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>

For example in the case above where m=3 and n=3

figure

<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">3</span>,<span style="color: #00ccff;">3</span>,<span style="color: #800080;">1</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">3</span>,<span style="color: #00ccff;">3</span>,<span style="color: #800080;">2</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">3</span>,<span style="color: #00ccff;">3</span>,<span style="color: #800080;">3</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">3</span>,<span style="color: #00ccff;">3</span>,<span style="color: #800080;">4</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">3</span>,<span style="color: #00ccff;">3</span>,<span style="color: #800080;">5</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">3</span>,<span style="color: #00ccff;">3</span>,<span style="color: #800080;">6</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">3</span>,<span style="color: #00ccff;">3</span>,<span style="color: #800080;">7</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">3</span>,<span style="color: #00ccff;">3</span>,<span style="color: #800080;">8</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">3</span>,<span style="color: #00ccff;">3</span>,<span style="color: #800080;">9</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
 

Not all subplots need to be the same size, the third element can be set as a row vector. In the case of wanting 3 small plots at the top and one large plot at the bottom the following can be used

figure

<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">3</span>,<span style="color: #00ccff;">3</span>,<span style="color: #800080;">1</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">3</span>,<span style="color: #00ccff;">3</span>,<span style="color: #800080;">2</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">3</span>,<span style="color: #00ccff;">3</span>,<span style="color: #800080;">3</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">subplot(</span><span style="color: #ffcc00;">3</span>,<span style="color: #00ccff;">3</span>,<span style="color: #ff0000;">[</span><span style="color: #800080;">4:9<span style="color: #ff0000;">]</span></span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>...<span style="color: #3366ff;">)</span>

I can change my code to plot the three charts in a 1 by 3 subplot by editing lines 2, 22 and 42 to add in subplot and to remove figure which was at lines 22 and 42.

[code language="matlab"]
figure
subplot(1,3,1)
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,2),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','nearest point interpolation','Location','NorthWest')
subplot(1,3,2)
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,3),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','linear interpolation','Location','NorthWest')
subplot(1,3,3)
% create a new figure
% x data is rawdata(:,1)
% y data is rawdata(:,2)
plot(rawdata(:,1),rawdata(:,2),'bo','MarkerSize',20,'MarkerFace','r','MarkerEdge','r')
% plot red circles with a red face and edge, size 20, no line
set(gca,'FontSize',20)
% change axes font size to 20
xlabel('time (s)')
ylabel('speed (m/s)')
title('speed of a rocket with respect to time')
% label figure
grid on
grid minor
% add grid lines
hold on
plot(dataint(:,1),dataint(:,4),'Color','b', 'LineStyle',':','LineWidth',5)
hold off
% Add the Interpolated Data Line to the Plot
legend('rawdata','cubic interpolation','Location','NorthWest')
[/code]

In this case the legend's don't look so great so I can change their Location from

<span style="color: #993366;">'</span><span style="color: #ff9900;">NorthWest</span><span style="color: #993366;">'</span>

to

<span style="color: #800080;">'</span><span style="color: #ff9900;">SouthOutside</span><span style="color: #800080;">'</span>

. To do this quickly I can use find and replace by pressing [Ctrl] and [ f ]

Octave struggles a little as the font size I am using is much larger than the default. :

Custom Plot Function

As you seen, by default Octave plots a line like:

Which isn't very aesthetically pleasing. Say we prefer plotting a red dotted line of Line Width 5, have axes of Font Size 20 and have both the major and minor grid lines on, we can create a function to do so. To create a function:

You will need to specify a name for the function. Function names follow the same rules as the rules for variables or for scripts, see my guide assigning variable names for more details.

We can remove all the comments at the top.

This will give you the following:

<span style="color: #3366ff;">function</span> <span style="color: #339966;">retval</span> = <span style="color: #3366ff;">plot1(</span><span style="color: #ff0000;">input1</span>, <span style="color: #ff0000;">input2</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">endfunction</span>

To make the code MATLAB compatible it is recommended to change

<span style="color: #3366ff;">endfunction</span>

to

<span style="color: #3366ff;">end</span>

so

<span style="color: #3366ff;">function</span> <span style="color: #339966;">retval</span> = <span style="color: #3366ff;">plot1(</span><span style="color: #ff0000;">input1</span>, <span style="color: #ff0000;">input2</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">end</span>

Functions have their own internal workspace which is separate from the main workspace. The only way to get variables into the functions internal workspace from the workspace is to use inputs and the only way to get variables back into the workspace from the functions internal workspace is to use outputs.

Since we are looking to create a plot and not wanting any output variables in the main workspace we can remove the output variable thus:

<span style="color: #3366ff;">function</span> <span style="color: #3366ff;">plot1(</span><span style="color: #ff0000;">input1</span>, <span style="color: #ff0000;">input2</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">end</span>

And since the input variables we want are going to be x data and y data respectively we might as well call them

<span style="color: #ff0000;">x</span>

 and

<span style="color: #ff0000;">y</span>

.

<span style="color: #3366ff;">function</span> <span style="color: #3366ff;">plot1(</span><span style="color: #ff0000;">x</span>, <span style="color: #ff0000;">y</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">end</span>

Once this function is created if we type in

<span style="color: #3366ff;">plot1(</span><span style="color: #ff0000;">a</span>, <span style="color: #ff0000;">b</span><span style="color: #3366ff;">)</span>

The function will set the variable

<span style="color: #ff0000;">a</span>

 from the workspace to the variable

<span style="color: #ff0000;">x</span>

in the functions workspace. If there is a variable 

<span style="color: #ff0000;">x</span>

specified in the workspace it will not influence the variable 

<span style="color: #ff0000;">x</span>

specified in the functions workspace. Likewise if there is a variable 

<span style="color: #ff0000;">a</span>

in the function's workspace it will not influence the variable 

<span style="color: #ff0000;">a</span>

in the workspace. Now that we have the input arguments which have been set to

<span style="color: #ff0000;">x</span>

and

<span style="color: #ff0000;">y</span>

respectively, we can now put plot commands in the function's workspace data using

<span style="color: #ff0000;">x</span>

and

<span style="color: #ff0000;">y</span>

:

<span style="color: #3366ff;">function</span> <span style="color: #3366ff;">plot1(</span><span style="color: #ff0000;">x</span>, <span style="color: #ff0000;">y</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span><span style="color: #ff0000;">x</span>,<span style="color: #ff0000;">y</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">r:</span><span style="color: #993366;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #993366;">'</span>,5<span style="color: #3366ff;">)</span>;
<span style="color: #3366ff;">set(</span>gca, <span style="color: #993366;">'</span><span style="color: #ff9900;">FontSize</span><span style="color: #993366;">'</span>,20<span style="color: #3366ff;">)</span>;
<span style="color: #3366ff;">grid</span> <span style="color: #ff0000;">on</span>;
<span style="color: #3366ff;">grid</span> <span style="color: #ff0000;">minor</span>;
<span style="color: #3366ff;">xlabel(</span><span style="color: #993366;">'</span><span style="color: #ff9900;">x</span><span style="color: #993366;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">ylabel(</span><span style="color: #993366;">'</span><span style="color: #ff9900;">y</span><span style="color: #993366;">'</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">end</span>

We can use the function by typing in

<span style="color: #3366ff;">plot1(</span><span style="color: #00ccff;">dataint(</span><span style="color: #ff0000;">:</span>,<span style="color: #ff0000;">1</span><span style="color: #00ccff;">)</span>,<span style="color: #00ccff;">dataint(</span><span style="color: #ff0000;">:</span>,<span style="color: #ff0000;">2</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

in the command window and pressing [Enter].

<span style="color: #3366ff;">plot1(</span><span style="color: #00ccff;">dataint(</span><span style="color: #ff0000;">:</span>,<span style="color: #ff0000;">1</span><span style="color: #00ccff;">)</span>,<span style="color: #00ccff;">dataint(:</span>,<span style="color: #ff0000;">3</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">plot1(</span><span style="color: #00ccff;">dataint(</span><span style="color: #ff0000;">:</span>,<span style="color: #ff0000;">1</span><span style="color: #00ccff;">)</span>,<span style="color: #00ccff;">dataint(</span><span style="color: #ff0000;">:</span>,<span style="color: #ff0000;">4</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

Or using other data instead such as the raw data

<span style="color: #3366ff;">plot1(<span style="color: #00ccff;">raw</span></span><span style="color: #00ccff;">data(</span><span style="color: #ff0000;">:</span>,<span style="color: #ff0000;">1</span><span style="color: #00ccff;">)</span>,<span style="color: #00ccff;">rawdata(</span><span style="color: #ff0000;">:</span>,<span style="color: #ff0000;">2</span><span style="color: #00ccff;">)</span><span style="color: #3366ff;">)</span>

 

[code language="matlab"]

% This will plot a dotted red line of thickness 5.
% The graph will have gridlines and the font size will be 20
% x and y labels are applied
% save as plot1
function plot1 (x, y)
plot(x,y,'r:','LineWidth',5);
set(gca, 'FontSize',20);
grid on;
grid minor;
xlabel('x')
ylabel('y')
end
[/code]

Add notes on axis

axis('tight')
axis('square')

Add notes on

 

myfig1=figure

myplot=plot(x,y)

Get Current Axes (gca)

Get Current Figure (gcf)

Get Current Object (gco)

myplot.Color
myplot.MarkerFaceColor
mplot.xData
myplot.yData

gca.XAxisLocation='top'
gca.XTick=[1,2,3,4]
gca.XTickLabel={'1','2','3','4'}
gca.XTixkLabelRotation=45

 

ax1=axes

ax2=axes('position',[left,bottom,width,height])