Skip to main content

3.1) Declaring Variables


  • Variables are created easily within Matlab
  • Variables are named memory locations which we use to store data
  • To define a variable we give it a name and a value. For example:

[matlab]
>> a=3
>> b='hello world'
>> c=2^10
>> d=4+(2*6)
[/matlab]

  • Variable names begin with a letter and can contain up to 64 characters
  • Remember before when we were using Matlab as a simple calculator, Matlab would assign the answer to the calculation to the variable "ans". Subsequent calculations at the command prompt would make Matlab overwrite the variable "ans" with the most recent answer
  • The value of the variable (3, 'hello world', 2^10, 4+(2*6)) can take a number of types (or specifically classes). Here, variables a, c and d are double precision numbers of the Numeric class, whilst b is a string of class Char
  • We will talk more about data classes later