# Introduction to Variables

1.9.1 What are Variables?
A variable can be defined as a data store. From maths, you will recall that you can have ‘unknowns’ that can be assigned values to satisfy equations. Those unknowns are variables, and depending on the equation, these variables can hold different values (in the case of polynomials, for instance). In programming, it is very much the same, but you can have different types of variables for use in different applications. We will first look at the main different data types and we will then explore when each might be relevant. We will then look at when those types of data can be stored into variables.

Data can be stored as a:

• String (alphanumeric). These can be used to store names, flight numbers, etc.
• Number (real, integer). These can store years of birth, the square root of a number to 10 decimal places, etc.
• Boolean (true/false). These can show that something has one of two outcomes.
• Currency
• Date/time

and so on.

MATLAB treats everything numerical as a matrix. Therefore, all variables used in MATLAB are stored as matrices of the dimensions of the data input. For instance, if you define variable x to hold a value of 2, MATLAB will store a 1×1 matrix into the workspace with a value of .

Arrays (that store multiple data into one variable) in MATLAB are similar to matrices but they have their differences in notation and in indexing. Those will be covered in the matrices section of this series.

You might sometimes need to convert certain data types from one form to another, and that can be done using commands. For instance, if you would like to convert a number to a string, you’d use the `num2str(variableName)` command. This will be needed in the Printing Exercise at the end of the Introducing MATLAB section. Open the documentation and find out how to convert other data types from one form to the other.

Recalling Exercise 1.2, it was seen that you had to use quotes to store strings using the `disp()` command. This is because typing in strings without quotes will automatically qualify what you wrote as a variable, and in the case that it wasn’t previously introduced into the workspace, that variable would be null (it would contain no value), and you’d hence get an error. Quotes tell MATLAB that your input is a string input, and that therefore tells MATLAB to assume that anything entered will not be a variable.

What if we did not want to do it that way? Inserting quotes is not always ideal and when we know that a certain input will be a string, is there a way of telling MATLAB to also assume that the input is a variable without using quotes? The answer is yes. Have a look at the modified code below.

```% Test Program
% Written by: Full name

disp('hello - who are you?')
name = input('Please enter name' , ‘s’);
d = date;
answer =['Hello ' name '. Today is ' d ','];

You may not have immediately spotted the difference. However, if you look at the second name input, you will notice the presence of a comma and a ‘s’. This tells MATLAB that the input will be a string, and that will automatically get MATLAB to input quotes around the text that you enter, without you needing to input any by yourself.

1.9.2 Introducing, Editing and Calling Variables
For those of you who have had some experience programming, you may be wondering how variables in MATLAB work. You may have already noticed through the examples that variables in MATLAB do not need to be declared explicitly because MATLAB does that by itself. This means that you need to be extra careful when you program.

To introduce a variable, you would simply type in the name of the variable that you wish to declare and give it a value, for example:

`age = 21`

This will set a variable called `age` to hold a value of 21. You can perform this operation of declaring and assigning a value to a variable through the execution of a script or by simply typing the command into the Command Window. However, it is recommended to type all your commands into a script that can be easily modified and accessed at a later point in time.

To edit a variable, you would simply change its value. For example, a year on, the value of `age` becomes 22. To change its value, you would use this command:

`age = 22`

To call a variable, you would type its name into the Command Window, or in a script. This will introduce everything stored in the variable into the session and will enable to you perform operations with the introduced variable.

To remove a variable, you can delete it from the Workspace, or you can type the command:

`clearvars variableName`