# Introduction

This continues on from Working with Scalars and Using MATLAB/Octave as a scientific calculator. I am using Octave which is the open source version, available for everyone to use. The user interface of MATLAB and functionality is superior (Octave seems to be like MATLAB 10 years ago) however they use the same underlying language and I have wrote these guides taking you through the basics and thus these guides are equally applicable to both products.

This takes you through setting Variables in Octave/MATLAB and the user interface which includes the File Browser, Workspace, Command Window, Command History and Variable Editor. As this particular guide is to get you comfortable with the user interface, I have went slightly overboard on the screenshots, you can scroll through them very quickly when reading through however.

# Octave User Interface

When you open MATLAB/Octave there will be the Command Window. If we type

x=1

Then press [Enter]

$\displaystyle x=1$

Then we will notice that under the workspace shows up x with the value 1. In other words we have assigned the value of 1 to the variable x. For this the variable x has to be on the left hand side of the equals sign and the value that the variable is assigned to has to be on the right hand side.

Also notice that the following symbols appear:

>>

This is called the prompt and denotes that we have started a new line in the Command Window in which we can type code.

In the Command Window 1=x does not make sense as 1 is a number that cannot change it’s value and x is an unassigned variable. Inputting this will give an error.

We can also double click the variable x on the workspace which will open the Variable Editor to the right hand side, displaying the data of the variable x in spreadsheet format:

The variable editor to the right hand side opens up the variable x showing it’s value in a spreadsheet. We only assigned a single number to x, known as a scalar.

We can create a second variable y and assign it a value of 2 by typing in:

y=2

$\displaystyle y=2$

Typing in y=2:

Pressing [Enter] and the variable y is created and assigned the value of 2:

Once again the following symbols appear:

>>

Denoteing that we have started a new line in the Command Window.

# Variable Names

You can use a large assortment of variable names however there are a few rules:

• Variable names can contain numbers and letters however must begin with a letter
• Cannot have special characters such as ( ) [ ] { }, ; + – / * ^ or spaces as MATLAB/Octave use these. They can however contain the underscore _
• They are case sensitive Var≠var
• i, pi, e, log, sin, asin, cos, acos, tan, atan, max, min, plot, clear, history, clc should not be used as these are inbuilt variables

To check if a variable exists one can use the command:

exist variable

If the value returned is 0, you can go ahead and use that variable. If the value is non-zero you should be careful because it’s defined elsewhere. In particular the value returned of 5 it is an inbuilt Octave/MATLAB function. Unsurprisingly most of the key words used before return a value of 5:

If the value returned is 1 then it is a variable already existing in the workspace, in which case if you assign that variable again it will override the original value.

# Mathematical Operations Using Variables

We can work with variables just like they are numbers. Thus we can perform addition, subtraction, multiplication, division, use exponents on the variables, or alternatively use the variable as an exponent. We can also use variables as input arguments to functions such as sqrt(xwhich we seen earlier and of course assign the calculations to a new variable. Thus:

z=x+y

$\displaystyle z=\underset{{x=1}}{\mathop{x}}\,+\underset{{y=2}}{\mathop{y}}\,=1+2=3$

Typing in z=x+y and pressing [Enter]

Gives the new variable z=3 which is calculated by summing together x+y. x and y at that moment in time have the values x=1 and y=2.

Now supposing we were to change x and set the variable

x=4

$\displaystyle x=4$

Typing in x=4 and pressing [Enter]

z was calculated using the previous variables $\displaystyle x=1$ and $\displaystyle y=2$ with $\displaystyle z=x+y=1+2=3$ and the value 3 was assigned to the variable z which is stored in memory. It is left unchanged unless it is explicitly reassigned like x just was.

With this notation it thus becomes possible to update a variable using it’s former value. For example:

x=4 x=x+1

$\displaystyle \underset{\begin{smallmatrix} \text{updated value} \\ \text{of variable} \\ x \end{smallmatrix}}{\mathop{x}}\,=\underset{\begin{smallmatrix} \text{former value} \\ \text{of variable} \\ x \end{smallmatrix}}{\mathop{x}}\,+1$

x is set to a value of 4:

x=x+1 is input:

x is reassigned with the new value 5:

This can be a bit confusing to get your head around initially, so we can have a look at this in more detail, adding in temporary variables. let’s change x back to 4 and start again:

Lets now create a new variable called oldx and assign it to the value of x. To do this we type in oldx=x

x is equal to 4 at that moment in time so oldx=4. We will see oldx appear in the workspace after pressing [Enter] and the >> symbols indicating we are on a new line.

Now we can create a new variable called newx and set newx=oldx+1 and then press [Enter]. At this moment in time oldx=4 and so newx=4+1=5

Now in the last step we can set x=newx and press [Enter]. Now x is updated to the value of 5.

In more detail, this operation introducing temporary variables is:

x=4 oldx=4 newx=oldx+1 newx=4+1 newx=5 x=newx x=5

# Suppressing Output

If we want to assign a variable but not output it’s result in the Command Window we can end the line in a semicolon;

x=x+1;

While we are still just working with scalars (single numbers) there is little advantage in doing this however if you are working with far more complicated arrays, you may not want every single value clogging up your command window. Moreover the time it takes Octave/MATLAB to display every number in the Command window may substantially increase the time it takes to compute a more complex calculation.

Looking at the variable editor we can see that x has the value of 5. We can input x=x+1; this time ending with a colon:

When we press [Enter] we can see the >> meaning we have moved onto a new line and we can see the value has been updated to 6 in the workspace and variable editor. It is not hwoever input into the command window. This means Octave/MATLAB has performed the operation but suppressed the output so it is not printed in the command window.

# The Workspace

By default Octave opens in C:\Users\YourName and MATLAB opens in C:\Users\YourName\Documents\MATLAB. You can change the folder by right clicking Documents and select make new directory. A directory is just another name for a folder. You can then name this folder and navigate to it. You can save all the variables in a workspace to file by using

save workspace

This will create a workspace file in your selected directory.

The default directory in Windows 10 is C:\Users\YourName

We can right click the documents folder:

Then select New Directory (folder):

Here you can name the folder and then select OK:

Now go into the Documents folder:

Note how the current directory now states C:\Users\YourName\Documents Now you can select your new folder:

The current directory (folder) should be the folder you selected:

To save all the variables to file we can input

save workspace

where workspace is the name of the workspace you are saving.

Once you have pressed [Enter] you should see the workspace appear in the folder

The command

clear oldx

will clear the variable oldx from the workspace.

clear all

will clear all variables existing in the workspace.

The saved workspace can however be reopened by typing in the command

load workspace

In the workspace we cans see our temporary variable oldx we can clear it by typing in in clear oldx

Once we press [Enter] we’ll get to a new line with the symbols >> and we’ll see that the temporary variable oldx is no longer in the workspace.

We can repeat this with the temporary variable newx

If we want to clear the entire workspace we can use the command clear all

You’ll see that now all the variables are gone. To get them back we can load the workspace from the file workspace by typing in load workspace

All 5 variables have now returned:

However I don’t want the temporary variables oldx and newx. I can clear these individually again and create a new workspace2. If I load this instead of workspace I will get only my three variables x, y and z:

clear oldx clear newx save workspace2

Now I can use:

clear all

This will clear all the variables.

You can see the 3 variables x,y and z show in the workspace.

# The Command Window

Saving the Workspace doesn’t save anything in the command window, only the variables in the Workspace. The commands in the command window can instead be copied and pasted into a script file:

To the left you can select new script:

This will give you a blank script file:

Right click it and select paste:

Remove the lines that were outputted to the Command Window and the >> prompts before your code:

Now you can press the save button:

You’ll need to name the script file. It has similar naming rules to variables:

Script files will show in the folder/directory:

Code can quickly be retyped in the command window by using the up arrow [↑], which will take you to the last line of code typed into the command window, pressing [↑] will take you to the second last line of code and pressing [↑] will take you to the third last line of code etc. If you go past the line of code you want to type, you can always use the down arrow [↓] to move back to it. Once you have the line of code you wish to re-enter, press [Enter]. Alternatively modify it slightly as desired and press [Enter].

To get back to the command window from the script editor, select command window at the bottom.

Pressing [↑] will give the last command, save workspace

Pressing [↑] again will give the second last line of code newx=oldx+1

Pressing [↑] will give the command before that oldx=x

Pressing [↑] again will get to the line of code before that x=4

Pressing [↑] again will get to the line of code before that x=x+1 which is the code I want to re-execute. If I overshoot and want to go back to a later line of code I can instead press the down arrow [↓].

I will now press [Enter] to re-execute it:

The code has been re-executed and I have the new prompt >>

I can press the [↑] arrow again to get to the next line:

To clear the Command Window you can use the line

clc

This will not clear the command history, to clear this use the line

history -c

We can see the list of commands in the command window. Typing in clc

Then pressing [Enter] we can see that the command window is now clear:

The command window history still shows however. We can likewise clear this using history -c

Once we press [Enter] we can see the command window history is gone. We can clear the line history -c by using clc again.

Note how clearing the command windows and the command history leaves the workspace untouched. To clear that we must use the command clear all as discussed above.

# Scripts

If writing long lines of code, it is better to use a script file opposed to typing everything separately into the Command Window. After using the clear all command to clear the workspace. One can run the script which will recreate the variables x, y, z, oldx and newx.

To get to the script, double click it which will take you to the Editor.

To launch the script right click it and select Run, alternatively type the name of the script into the Command Window.

The script file is in the folder. Note the command window, workspace and command history have all been cleared. The script can be opened in the script editor by double clicking it:

This will take you to the Editor window, see the editor window tab at the bottom:

To launch the script, you can right click it in the folder and select Run

You can see this script has ran as all the variables now appear in the workspace:

One can return to the command window and see what is output:

Because no lines of code ended in ; all the results from the line of code were shown in the command window. However the command lines were not shown as they are in the script.

# Suppressing Output in a Script

The script can be modified to suppress the output of each line by ending each line in ;

Let’s clear all variables using clear all

Now open the script in the script editor and modify each line to end in a ;

After modification the script file is unsaved. Meaning if it’s run the older version of the script will run. You can see a script’s changes haven’t been saved if there is a * present in it’s name in the top left tab of the script editor:

After clicking the save button the * is gone:

We can return to the command window using the tab at the bottom:

Now we can run the script again:

Notice the difference. All the variables have been created as before however the only line in the command window this time is the VariableTestScript which is the name of the script. Scripts can be launched in the command window by typing their name

Commands such as clear all, clear variable, clc and history -c can be included in the scripts. If I had wanted to I could have included

clear oldx

clear newx

In the script which would have removed the temporary variables.

# Commenting in a Script

Any line beginning with a % is taken as a comment and %% begins a new section. These lines will automatically become green and are ignored by Octave/MATLAB when launched. They are instead notes to help the user understand their code.

% Creates the variables x and y
x=1;
y=2;
% Creates the variable z from x and y
z=x+y;
x=4;
% Updates the variable x using the original value of x
x=x+1;
% Returns x back to 4
x=4;
%% Looks at x=x+1 in more detail
oldx=x;
newx=oldx+1;
newx = 5;


Any line that is commented out is green and will be ignored by Octave/MATLAB, comments are notes for the user only.

# Layout

In Octave all the Windows can be undocked and or all closed. This is a feature especially useful for those who are running multi-monitor configurations however it often confuses new users. If you have undocked or closed a window by mistake you ca always use the windows menu to restore it., If you’ve made a complete dogs breakfast of it, you can always rest the default layout.

Let’s undock the command history for instance:

Here it is as an undocked window:

Playing around, I end up in a mess:

I can however go to window and then reset the default layout:

Which gives me this – for some reason I need to drag the variable editor to the right to get the actual default layout: