# 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.

### Basic Calculations

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

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”.

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:

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

### 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:

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_13 – Valid
• Maximum_Voltage – Valid
• 1st_number – Invalid 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:

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:

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: using variables for symbolic (i.e. a + a = 2a) calculations is different and will not work like this. 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.

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.

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: