320 likes | 331 Views
Announcements. Exercise Sets 2 and 3 Due Thursday. MatLab m-file. M-file : Used to Store Commands Script : list of Commands to Run as if Typed Functions : Code that Call Be Called Later On. MatLab Scripts. Can Be Called from Command Window Named “ scriptName.m ”
E N D
Announcements • Exercise Sets 2 and 3 Due Thursday
MatLab m-file • M-file : Used to Store Commands • Script : list of Commands to Run as if Typed • Functions : Code that Call Be Called Later On
MatLab Scripts • Can Be Called from Command Window • Named “scriptName.m” • Called from Command Window by Name (scriptName) • May Use “input” statement to Get User Input • Semi-colon (';') to Suppress Output • MatLab Version of a Program
Creating MatLab m-Files • Click on “New” in Upper Left Hand Corner • Opens Editor Window Above Command Window • Type Script (Code) in Editor Window • Save in “.m” File
Running MatLab m-Files • Type File Name (without “.m”) in Command Window • Runs All Statements in .m File
MatLab Errors • Syntax Error : Error in the Script Grammar • Missing parenthesis • Misspelled identifiers • Semantic Error : Error when Running the Script (also called Runtime Error) • Miscalculation • Unexpected Results
Selection (if-then-else) • Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else): Control Proceeds Dependent on Conditions Iteration (looping): Control Repeated until Condition Met
MatLab if Statement Syntax • Syntax if condition statements; end; • If condition Is True, statements Are Executed • If condition Is False, statements Are Not Executed
Conditional Operators • Relational Operators: < , > , >= , <= • Equality Operators: == , ~= • Comparing Strings : • strcmp(str1, str2) • Returns true or false
Selection Examples if age < 30 disp('You are very very Young'); end if strcmp(grade , 'A') disp('Congratulations!'); end if ~strcmp(grade,'F') disp('You passed!'); end
else Statement • Syntax: if (condition) statement(s);//condition true else statement(s);//condition false end
if-else Example if myGrade >= 60 disp('You passed!'); else disp('How about them Cubs?'); end
Nested if Statements if myGrade >= 80 if myGrade >= 90 disp('You have an A!'); else disp('You have a B!'); end else disp('We''ll give you a C.'); end
if/else (Cascaded) if myGrade > 90 disp('A!'); else if myGrade > 80 disp('B!'); else if myGrade > 70 disp('C!'); else disp('Oh-oh!'); end end end
Logical Operators • A Boolean Expression Is an Expression with a Value of Either True or False • A Logical Operator Is One Used to Further Specify True or False in an Expression • Connects Two or More Expressions Together • && Is Logical “AND” • || Is Logical ‘OR” • &&: Both Operands Must Be True for Entire Expression to Be True • ||: Only One (or Both) of the Operands Must Be True for Entire Expression to Be True • ~ is Logical “Not” (Take Opposite of Boolean Expression)
Logical Operators if numStudents > MIN && numStudents < MAX classRun = 1; else classRun = 0; end if numStudents > MAX || numInstructors == 0 classRun = 0; end if classRun disp('Class will Run!'); end
Operator Precedence • () • ~ (not) • *, /, % • +, - • <, <=, >, >= (Relational Operators) • ==, ~= (Equality Operators) • && (Logical AND) • || (Logical OR) • = (ASSIGNMENT)
Multiple Logical Operators if num1 > MAX || num2 == 0 && num3 == 0 disp('num1 is MAX or something is 0'); end disp('I think…..');
Switch Statements • Also Called Switch/Case Statement • Just Case in Other Languages • Selects Among Several Different Actions • If Value Is Matched, Statements under Control of that Case Block Are Executed
Switch Case Example n = input('Enter a number: '); switch n case -1 disp('negative one') case 0 disp('zero') case 1 disp('positive one') otherwise disp('other value') end
Switch Case Example BASERATE = 200; n = input('Enter Passengers: '); switch n case 2 rate = BASERATE * 0.80; case 4 rate = BASERATE * 0.75; case 5 rate = BASERATE * 0.55; otherwise rate = BASERATE; end out1 = sprintf('Your rate is %.2f',rate); disp(out1);
Switch Case Example BASERATE = 200; n = input('Enter Passengers: '); switch n case {2,3} rate = BASERATE * 0.80; case 4 rate = BASERATE * 0.75; case 5 rate = BASERATE * 0.55; otherwise rate = BASERATE; end out1 = sprintf('Your rate is %.2f',rate); disp(out1);
Switch Case Example cmd = input('Enter a command: ','s'); switch cmd case 'left' disp('going left') case 'right' disp('going right') case 'up' disp('going up') otherwise disp('unknown command') end