GEOSC 444
Matlab Application for Geoscience

Lesson 1: Fire it up

PrintPrint

Syntax introduced:

+=ans;whosiskeyword

Once you've installed MATLAB and started it, or logged into it through PSU's webapps, you are presented with an interface that looks like this.

MATLAB interface screenshot
MATLAB 7 command interface

The central panel is the Command Window, where you can type what you want MATLAB to do. Go ahead and put your cursor in there right now. Type an obvious math problem such as 3 + 4 and then hit return. You should get back:

>> 3+4 ans =      7 >>

with a blinking cursor after the last >>

Good! You're on the way to being an expert already. The ans is short for "answer" and it is also a temporary variable that you could use in the very next command. Try typing ans+2 and you'll see that MATLAB will reassign the value 9 to ans.

Assigning variables

To assign a variable in MATLAB first type the name of the variable and then use a single equals sign to set that variable equal to whatever value you want to give it. For example, to make a variable called "a" that is equal to 3, type a=3 into the command window. MATLAB tells you:

>>a=3
a =
    3
>>

If you don't want MATLAB to repeat everything back to you, type a semicolon at the end of your command, as in a=3;

You can also tell MATLAB to assign the results of a calculation to a variable. For example, type b=a+4;

MATLAB has now behind the scenes assigned the value 7 to b. If you look over at the Workspace panel on the right, you'll see a listing of variables there. Another way to find out what variables are currently in use the the command whos. If you ever want to know the value of a variable without looking at the Workspace panel you can just type the name of the variable and hit return.

Rules about naming variables

Variables can't start with a number and cannot have spaces in them, but they can be a whole word or phrase as long as you jam the words together. Most people use underscores or camel case for readability. Variables are also case sensitive. Some people like to use conventions such as capitalizing matrices and using lowercase for vectors and scalars. MATLAB doesn't care how you go about it, but sometimes adhering to a consistent naming scheme makes your programs more human-readable, thus making you a more popular collaborator.

Variable name checklist
Variable OK? notes
x yes
X yes capital X is a different variable than lowercase x
1x no don't start a variable name with a number
x1 yes variables can have numbers in their names as long as they aren't first
eliza is cool no variable names can't have spaces, even if the sentence is a true statement
eliza_is_cool yes totally
elizaIsCool yes camel case is fine
eliza.is.cool no don't use punctuation other than the underscore because most of them have special values
for no don't use any keywords that are on the reserved list. the command iskeyword tells you what's on the list
fourScoreAndSevenYearsAgo yes but there isn't room for the whole Gettysburg Address because variable names can have a maximum of 63 characters

Check yourself!