MATLAB tokens and expressions ------------------------------------------------------------------------------- (0) Statements... what you need to know for now: + MATLAB reads commands left-to-right, top-down + MATLAB looks for a RETURN before going to the next statement + statements can be entered in the command-window or in M-Files (1) white space + blank spaces + supply as many as you wish between language elements + do not separate within a command: ex) he lp + hitting RETURN does not count as whitespace! (2) comments + MATLAB ignores anything following a % sign on the SAME line + no nesting of comments + to comment large block, use Text-->Comment in M-File editor (3) numbers + languages like to have have quantities + usually separate integers and decimal quantities, but every number in MATLAB is decimal (called FLOATING POINT) + examples: 1 % integer (whole number) 1.1 % floating point number (decimal) 1e2 % floating point number (scientific notation) 27i % imaginary 27j % imaginary (4) operators + use special characters and some words to describe how to perform tasks on values, like numbers + see $help ops$, $help arith$, $help slash$, $help relop$ + examples: 1+2 % add 1-2 % subtract 1*2 % times 1/2 % divide 1^2 % power (5) identifiers + also called names, variables + token that stores another quantity in memory + acts like a placeholder for a value + also used as labels for other constructs, like function names (will see later) + rules: - case-sensitive (for example, $A$ does not mean $a$!) - only 31 characters (can go more, but extras are ignored) - start with a letter - characters may include digits (0-9) and the underscore (_) examples) x, x1, x_1 - some identifiers have predefined values + examples of use: >> x >> x = 1 >> x >> x = 2; >> x >> pi ------------------------------------------------------------------------------- (6) punctuation + separate other language elements comments: % suppress output: ; separate statments: , + For a full list, see $help punct$ examples) 1+1 % Hi! % evaluate 1+1 and ignore the comment a = 1; % assign 1 to $a$ and suppress output a % check the value of $a$ (MATLAB will say 1) a=1, b=2 % you may use a comma to separate statements % on the same input line (7) keywords + also called reserved words + part of the language + cannot change meaning using assignments + $if$, $end$, $while$, $for$ (and more to come!) + enter $iskeyword$ for full list + Keywords are involved with programming in a big way.... (8) strings + STRINGS are collections of individual characters (usually ASCII) + They are not names and not keywords. + Strings are used for processing text files and providing labels. + To create a string surround characters with single forward quotes: 'hello, I am a string!' % enter a string + Characters inside strings have numerical ASCII codes. + To find out a string's numerical codes, use $double(string)$: x = 'abc'; % assign a string to $x$ double(x) % determine the string's numerical character codes + To find what character a number corresponds to, use $char(number)$ char(100) % produces the string d (9) functions + a FUNCTION is an expression with the syntax $name(args)$, where $args$ is a sequence of variables separated by commas. + Entering a function tells MATLAB to run another program that performs the tasks the function represents. example) sqrt(4) % take the square root of 4 % MATLAB reports that the answer is 2 + You may include a function in an expression. + MATLAB performs the desired tasks in the function. If the function produces a value (known as returning a value), MATLAB substitutes the returned value for the function. example) 1 + sqrt(4) % add 1 to square root of 4 % MATLAB will replace sqrt(4) with 2 and then add 2 to 1 + Some functions in MATLAB take may no arguments example) help (10) Arrays and matrices + everything in MATLAB is an array + an array is a collection of data, like a table or spreadsheet + in other languages, an array is NOT a language element + use [] and : examples) [1 2 3 4] 1:4 v = 0:100;