Variables and the workspace

# Variables and the workspace - Applied Mathematics...

This preview shows pages 1–7. Sign up to view the full content.

Variables and the workspace Week 2 Applied Mathematics Laboratory

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Variables Variables are fundamental to programming. In a sense, the art of programming is getting the right values in the right variables at the right time. A variable name (like the variable balance that we used in the previous week) must comply with the following two rules: 1. It may consist only of the letters a z , the digits 0 9 and the underscore ( _ ). 2. It must start with a letter.
A variable name may be as long as you like, but MATLAB only remembers the first 31 characters. Examples of valid variable names: r2d2 pay_day Examples of invalid names (why?): pay-day 2a name\$ _2a A variable is created simply by assigning a value to it at the command line or in a program, e.g. a = 98 If you attempt to refer to a non-existent variable you will get the error message ??? Undefined function or variable . .. The official MATLAB documentation refers to all variables as arrays , whether they are single-valued (scalars) or multi-valued (vectors or matrices) . In other words, a scalar is a 1x1 array, i.e. an array with a single row and a single column which, of course, is an array of one item.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Case sensitivity MATLAB is case sensitive , which means it distinguishes between upper- and lowercase letters. So balance, BALANCE and BaLance are three different variables Many programmers write variable names in lowercase except for the first letter of the second and subsequent words , if the name consists of more than one word run together . This style is known as camel caps , the uppercase letters looking like a camel’s humps (with a bit of imagination). For example, camelCaps , millenniumBug , dayOfTheWeek . Other programmers prefer to separate words with underscores . Command and function names are also case sensitive . However, note that when you use the command line help , function names are given in capitals, e.g. CLC , solely to emphasize them. You must not use capitals when running functions and commands !
The workspace Another fundamental concept in MATLAB is the workspace . Enter the command clear , and then run the compound interest program again Now enter the command who . You should see a list of variables as follows: Your variables are: balance interest rate All the variables you create during a session remain in the workspace until you clear them. You can use or change their values at any stage during the session. The command who lists the names of all the variables in your workspace

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Name Size Bytes Class balance 1x1 8 double array interest 1x1 8 double array rate 1x1 8 double array The function ans returns the value of the last expression evaluated but not assigned to a variable. The command
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/30/2011 for the course MECHATRONI EN11ME2050 taught by Professor Perera during the Spring '11 term at Asian Institute of Technology.

### Page1 / 37

Variables and the workspace - Applied Mathematics...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online