Download presentation
Presentation is loading. Please wait.
Published byMarcela Beranová Modified over 5 years ago
1
Announcements Exercise Sets 2 and 3 Due Thursday
2
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
3
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
4
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
9
Running MatLab m-Files
Type File Name (without “.m”) in Command Window Runs All Statements in .m File
12
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
16
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
17
MatLab if Statement Syntax
if condition statements; end; If condition Is True, statements Are Executed If condition Is False, statements Are Not Executed
18
Conditional Operators
Relational Operators: < , > , >= , <= Equality Operators: == , ~= Comparing Strings : strcmp(str1, str2) Returns true or false
19
Selection Examples if age < 30 disp('You are very very Young'); end
if strcmp(grade , 'A') disp('Congratulations!'); if ~strcmp(grade,'F') disp('You passed!');
20
else Statement Syntax: if (condition) else end
statement(s); //condition true else statement(s); //condition false end
21
if-else Example if myGrade >= 60 disp('You passed!'); else
disp('How about them Cubs?'); end
22
Nested if Statements if myGrade >= 80 if myGrade >= 90
disp('You have an A!'); else disp('You have a B!'); end disp('We''ll give you a C.');
23
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
24
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)
25
Logical Operators if numStudents > MIN && numStudents < MAX
classRun = 1; else classRun = 0; end if numStudents > MAX || numInstructors == 0 if classRun disp('Class will Run!');
26
Operator Precedence () ~ (not) *, /, % +, -
<, <=, >, >= (Relational Operators) ==, ~= (Equality Operators) && (Logical AND) || (Logical OR) = (ASSIGNMENT)
27
Multiple Logical Operators
if num1 > MAX || num2 == 0 && num3 == 0 disp('num1 is MAX or something is 0'); end disp('I think…..');
28
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
29
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
30
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);
31
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);
32
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.