%------------------------------------------------------------------------------- % CS100J, Spring 2001 % Thurs 4/19 % Lecture 24 %------------------------------------------------------------------------------- % This file will run in MATLAB! %------------------------------------------------------------------------------- % Notation: % + The percent symbol (%) means that MATLAB treats everything that follows as a % comment which is ignored by MATLAB. % + Anything surrounded by dollar signs ($something$) means a portion of MATLAB % or a MATLAB command that you would enter. % + menus: "->" means selecting a submenu % example) File->Set Path means "click on File menu then click on Set Path % submenu" % + you might wish to uncomment one of the following two lines: % more on % turns pager on % more off % turns pager off %------------------------------------------------------------------------------- % Programming in MATLAB (Matrix Laboratory) % + two places to put MATLAB statements: % - Command Window (workspace) % - Edit Window (M-File) % % Command Window % + MATLAB's "main" window % - enter commands at prompt (>>) % - type a command and then press Return or Enter % - this is the "brain" % + carves up memory for programs % + acts upon commands and lines of code % + The "brain" is called the MATLAB workspace or base workspace: % - processes and evaluates commands % - remembers variables and commands you have entered % + key strokes for short cuts: % - recalling commands: up and own arrow keys % - editing editing commands: left and right arrow keys, Backspace, Del, % Home, something called "Emacs" % - interrupting evaluation: Control-C (hold Ctr key and press c) %------------------------------------------------------------------------------- % + "Help" commands % helpinfo % very general help (help on ways to get help) % help % get help on a command % helpwin % window contain help % helpdesk % more extensive HTML help browser % lookfor % search for keword % which % look for commands and files % demo % MATLAB demonstration % + Command options % - enter $help$ where is the name of a command % - many commands have options that modify the command's behavior % - example) more more on more off echo on echo off % - most commands here have options not shown! pause clc %------------------------------------------------------------------------------- % + Handy commands: % more on % turn on pager (spacebar) % clc % clear screen % clear % clear variable assignments % who % check current variables (see also File-> Show Workspace menu) % whos % long form of $who$ % format % set output preferences (see also File-> Preferences menu) % computer % type of computer % version % version of MATLAB currently used % ver % more explicit than $version$ % diary % save results of current session %------------------------------------------------------------------------------- % Edit Window % + want to enter multiple commands % + becomes tedious doing this in Command Window % + store statements in separate file called M-File (also called script file) % (script because MATLAB will read and process each statement in succession) % + create file, store statements, run "program" in Command Window % - create ASCII (also called text) files: % File->New->M-File or use text editor % - name of file is the program name % example) blah.m has the name $blah$ % - set path % + to load M-File into workspace, MATLAB need to know locaiton of file % + MATLAB has search path: a list of directories or folders where % MATLAB looks to find commands which are stored in M-Files % + You may see path by entering $path$ % + to add directory or folder to path, use File->Set Path, $editpath$, % or $addpath$ %------------------------------------------------------------------------------- % examples: % path % what are currently known paths % editpath % open pathbrowser to edit path % addpath % command line method % - miscellaneous directory commands: % pwd, cd % show current working directory for Command Window % place where MATLAB will store files by default % what % M-Files in current directory % type % show contents of M-File in Command Window % exist % check if M-file exists % dir, ls % show all files in current directory %------------------------------------------------------------------------------- % + saving work % - diary file: % % diary % open text file named diary in current directory % % save subsequent work in Command Window to diary file % - File->Print % + variables and files % - saving: % + menu: File->Save Workspace As % + CW: $save$ saves variables to file matlab.mat in MATLAB binary format % (unreadable to human, only MATLAB understands it) % + CW: $save$ saves variables to file called % + CW: $save$ ... saves jsut those variables % Many more options! see also "functional form" in $help save$ % - loading: % + menu: File->Load Workspace % + CW: $load$ (see $save$: many options!) % - deleting: % + CW: $delete$ %------------------------------------------------------------------------------- Language elements: pause clc echo on % Examples of tokens and expressions %============% % Separators % %============% % blank spaces % you may supply as many as you wish between language elements % do not separate within a command: example) he lp % example that works: help help % lots of spaces inserted and no change in behavior pause % special clc %==========% % Comments % %==========% % MATLAB ignores anything following a % sign on the SAME line pause %=========% % Numbers % %=========% 1 % integer (whole number) 1.1 % floating point number (decimal) 1e2 % floating point number (scientific notation) 27i % imaginary 27j % imaginary pause %===========% % Operators % %===========% 1+2 % add 1-2 % subtract 1*2 % times 1/2 % divide 1^2 % power pause % Operator precedence % Operator associativity % ---------------------- mostly the same rules % More information % ---------------- help arith help ops help slash help relop %===========% % Variables % %===========% % token that stores another quantity in memory % acts like a placeholder for a value % example) $x = 1$ stores the value of $1$ in variable $x$ % 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 % assignments % ----------- % To store a value in a variable, use an assignment statement % Syntax: $var = expression$ % MATLAB evaluates the expression, carves out a portion of memory, and % stores the value of the expression in $var$. % Other features: % + You may not use unassigned varibles. % + The equals sign (=) means ASSIGN in MATLAB, not equals! % + When talking of algorithms, write an assignment with an arrow. % example) "x <- 1" in English means $x = 1$ in MATLAB. % % More examples of assignments: % x % $x$ is not assigned a value, so MATLAB cannot evaluate x = 1 % assign $1$ to $x$ x = 2 % you change a variable's assignment x % once assigned, you may check a variable's assignment % recalling variable values % ------------------------- % You may use assigned variables in expressions. % Until you clear variables, restart MATLAB, or assign new values, % variable values remain in memory as long as your current MATLAB session % keeps running. % example) x = 1 % assign 1 to $x$ y = 2 % assign 2 to $y$ z = x+y % evaluate $x + y$ and store the result (3) in $z$ x = 4 % assign $x$ to a new value z % $z$ is still 3 and NOT 4+2 because $z$ % was already assigned BEFORE $x$ changed to 4 % predefined variables % -------------------- % MATLAB comes with useful value already built-in for your convenience! ans % default name for evaluated expressions without assignments pi % numerical pi eps % smallest number that can be added to 1 to make number bigger % than 1 inf % positive infinity NaN % not-a-number (ex: 0/0) nan % not-a-number (ex: 0/0) i % imaginary number (square root of -1) j % imaginary number (square root of -1) realmin % smallest usable positive number in MATLAB realmax % smallest usable negative number in MATLAB % You may actually change these values (usually with $eps$) eps=0.1 % Assign $eps$ to a new value (0.1) % To restore a predefined variable's value, see $clear$ below. % clearing variables % ------------------ % Sometimes you might wish to clear an assigned variable's value. % You may restart MATLAB with $clear$ clear % you may clear all variables of assignments % To clear just one variable, enter $clear var$: x = 1 % assign 1 to $x$ clear x % unassign $x$ % x % test value of $x$ % Use $clear$ to restore predefined variable values. % You may also enter $clear var$ to clear a specific variable: eps =0.1 % change value of $eps$ clear eps % restore $eps$ eps % see default value (2.2204e-16) %=============% % Punctuation % %=============% % 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 %==========% % Keywords % %==========% % also called reserved words % part of the language % cannot change meaning using assignments % if, end, while, for (and more to come!) % Keywords are involved with programming in a big way.... %=========% % 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 % You may also assign a string to a variable: x = 'blah, blah, blah' % assign a string to $x$ % 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 % Much more to say on this subject also.... %===========% % 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: help % Much more to say on functions.... %-------------------------------------------------------------------------------