Sample Codes to Try Out!

Here, you will be presented with a couple of exercises that we hope will give you an intuition of how MATLAB works.


Exercise 1.1
In the command window, type in the following commands, step by step and observe the Workspace as you enter each command. What is happening as you type? Don’t forget to press enter as you type in each line!
Do not simply copy and paste the below because you will not grasp what is happening if you do.

% Simple Sums
x = 25.0
y = 33.9
z = x * y
a = x + y
A = y / x
b = a * cos (30)
c = A * sin (30)
d = x ^ 2
D = sqrt(y)
e = sqrt(-1)


A few things to note:

  • MATLAB automatically declares variables that you did not previously declare.
  • MATLAB will not check to see if variables have been declared at the beginning of the code before executing. This means that you need to be extra careful when typing your code, otherwise you will get many errors due to typos. Make sure you log all your variables and their functions/purpose so that you don’t run into problems or get confused.
  • MATLAB will distinguish between upper-case and lower-case characters; it is case-sensitive! This applies to everything apart from comments. Note how ‘a’ is NOT the same as ‘A’. You can verify this by typing ‘a’ and ‘A’ into the Command Window and checking the output; they will be different unless it is a coincidence, and unless you force MATLAB to assign the value of ‘a’ to ‘A’ (a=A).
  • You can use the Command History window to group everything that you have typed in the Command Window to form an M-file that is easy to edit into a script.
  • The % symbol signifies a comment. MATLAB will ignore it and won’t incorporate it as part of your code.


Worked Exercise 1.1
Please follow the steps below.

  • Create an M-file (a script) using the Command History window. To do this, highlight the commands that you wish to include in your script and then right click and choose ‘Create Script’ from the context menu.
    Hint: To highlight all the commands, click on the line to the left of the commands. To only select some of the commands, hold ctrl on your keyboard and select the commands that you wish to include in your script. This is shown in Figure 1.11.
Figure 1.11

Figure 1.11

  • To get rid of the errors/warnings, add semicolons to end each line, as in Figure 1.12.
Figure 1.12

Figure 1.12

  • Save the script as ‘example.m’ and to run it, type ‘example’ into the Command Window and press the return key of your keyboard.

Hint: Naming MATLAB files has rules. MATLAB files must start with a letter and may contain:

  • letters
  • numbers
  • underscores

Tip: The command window has moved below the editor. To restore it back to its full size, close the editor by pressing on the small x next to ‘example.m’ at the top left of the editor.
You may be wondering why nothing seems to be happening after you type ‘example’ into the Command Window. To prove that something actually happens when you run the file, you should clear your workspace for this example (right click in the Workspace window and choose the option to clear it). Now run the M-file again by typing its name into the command window and notice what happens? This is an example of code without feedback; it is often good practice to make sure you give yourself feedback to ensure that the code actually works by getting the program to output text. That will be covered later.
There are many other ways to run files in MATLAB, including:

  • Clicking on the ‘Run’ option in the Editor
  • Right clicking on the file in the Current Folder window and selecting ‘Run’ from the context window
  • Typing its name (without an extension if it’s an M-file) into the Command Window
  • Calling it using other M-files or scripts