Introduction to Variables
Values that need to be stored for later use are given names. These are called 'variables' because they can hold any value and the value can be changed at any time:
>> my_age = 50;>> my_savings = 4500.66;These variables can be used anywhere you would use the value they contain, just type the name where you want to use it's value:
>> avg_savings_per_year = my_savings/my_age;>> my_age = my_age + 1;The values of the variables stay in memory in an area called the 'workspace' until you type:
>> clear or until you exit Matlab. To preserve the workspace just type:
>> saveand the next time you need it just type:
>> loadNB The statement my_age = my_age + 1 looks like it makes no sense, it cannot possibly be true! But the equals sign here is not being used as a logical comparison but as an assignment. To get a comparison operation we need to type:
>> my_age == my_age + 1Can you guess what the result of typing this will be? Try it. We will come back to comparisons later.