Posts tagged MATLAB

MATLAB Beginner’s Guide

This tutorial is meant to help you get started with MATLAB. It assumes you have never used MATLAB and will teach you the very basics of the program. Lets get started.

  1. Basic Calculations
  2. Creating Variables
  3. Using Variables
  4. Tips

Basic Calculations

When you first start up MATLAB your screen should look something like this

Matlab window after startup

For right now we will only be concerned with the “Command Window”. This is the main window at the center of the screen where you will be entering all of your commands. Let’s try to do a very simple calculation to start things off. Type in 2 + 2 (without the quotation marks) into the Command Window and push the ENTER key on your keyboard. I will be using quotation marks to denote input and output from MATLAB. The Enter key is used to run a command. As could be expected, MATLAB spits back 4.

>> 2 + 2
ans =
     4

By default MATLAB likes to spread out the output. To make it print the output more compactly type format compact and push ENTER. As long as vectors and matrices are not used, MATLAB pretty much behaves like a regular calculator. It follows the standard order of operations: parentheses -> exponents and roots -> multiplication and division -> addition and subtraction. An exponent can be entered by using the ^ key. Complicated and long expressions can easily be evaluated. For example:

>> ((10 * 2 - 5)^3)/(100.4 + 8^(1/2))
ans =
   32.6945

It is worth noting that MATLAB ignores spaces, however, I recommend using them to make the input easier to read.

Return to Top

Creating Variables

MATLAB allows the user to create variables. A variable is simply a symbol which can be assigned a value. For example A = 5. Variables in MATLAB do not need to be initialized like in some other programming languages. So, to assign a value to a variable, just enter the variable name followed by an equals sign, and then the value to be stored. Here are some examples:

>> B = 64.2
B =
   64.2000
>> pressure = 99
pressure =
   99

Variables are case-sensitive and can be named anything as long as they satisfy two conditions:

  • The name of a variable can not start with a number.
  • The name of a variable can only consist of letters, numbers, and underscores.

Here are some examples of valid and invalid variable names.

  • a13_13Valid
  • Maximum_VoltageValid
  • 1st_numberInvalid because it starts with a number
  • total_%Invalid because it contains an invalid character “%”
  • a/b Invalid because it contains an invalid character “/”

Expressions can also be assigned to variables. The expression is then evaluated and the resulting number is stored in the variable. For example:

>> A = (6^4 + 1) / 100
A =
   12.9700

As you can see, whenever you run a command MATLAB always prints some kind of output. Sometimes that output is necessary, like the output to an addition problem, but sometimes it just clutters up the workspace. In MATLAB to suppress the output of a command, put a semicolon ; at the end of the line before pushing ENTER. MATLAB will still do whatever you tell it to do, but it will refrain print any output. The use of this will become more apparent when you learn about scripts and functions. Here is an example that shows the difference between including a semicolon or not:

>> A = 5;
>> A = 5
A =
   5

In the case of assigning a value to a variable, the output is unnecessary so it makes the output more readable with the use of a semicolon.

Using Variables

Using variables is very simple. You can treat the variable like any other number when you write your expressions. Any place you can use a number, you can also use a variable. Note that using variables for symbolic (i.e. a + a = 2a) calculations is different and will not work like that. It is possible to do in MATLAB but requires the symbolic toolbox and will not be covered in this article. In the following example I assign the value 5 to the variable A, and then use the variable in an expression.

>> A = 5
A =
     5
>> 5 + A
ans =
    10

As you can see, MATLAB treated the variable A just like the number 5. Multiple variables can also be used in a single expression without issues.

>> A = 5
A =
     5
>> B = 2
B =
     2 
>> (A + 5) * B
ans =
    20

You might have noticed that the output sometimes displays ans = . MATLAB automatically saves the last output of a calculation into the variable ans. This makes it easier to break up calculations into multiple parts and use the answer in a following calculation. Here is an example ans in action:

>> 5 + 5
ans =
    10
>> ans^2
ans =
   100
>> ans - 20
ans =
    80

Tips

There are a few tips that are handy to know when working with MATLAB. If you want to clear all the text from the Command Window, use the command clc. If you want to remove a variable from the program memory, type clear variable_name and push ENTER (replace variable_name with the name of the variable you want to remove). More useful is the command clear all which will remove all the variables from memory. It’s useful for starting over.

Sometimes you want to repeat a previous command. To cycle through the past commands, push the “up-arrow” or “down-arrow” keys on your keyboard when the cursor is in the command line.

By default MATLAB only prints four digits of the output. This makes it easy to read but is sometimes not enough when a high degree of accuracy is required. If you need more digits in the output you can type format long to display up to 15 digits. Type help format in MATLAB to get more info about the different options you have for displaying output. The MATLAB help function is usually pretty useful and is accessed by typing help command_name, where command_name is whatever command you want to get more information about. MATLAB will display the help text right in the Command Window.

That concludes the tutorial. It only covered the most basic MATLAB topics.