Introduction to Programming in MATLAB Intro. MATLAB Peer Instruction Lecture Slides by Dr. Cynthia Lee, UCSD is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. Based on a work at www.peerinstruction4cs.org. 1 A number by any other name wouldn’t smell as sweet? VARIABLE NAMES 2 Which of these is NOT a legal variable name in MATLAB? a) b) c) d) e) _factors_of_48_ my_factors factor f None or more than one of the above 3 An important programming language concept called “scope” refers to the process for managing names, and how to resolve conflicts where different things have the same name. We’ll talk more about this later, but for now just something you should be aware of. 4 Computer scientists call words/text/sentences/punctuation “strings” STRINGS IN MATLAB 5 Strings in MATLAB • Remember: to a computer, EVERYTHING is numbers – Numbers, letters, words, punctuation, images, video, sound – Even computer programs themselves! • In your lab, you got MATLAB to “reveal” to you its code numbers that it associates with each character in a string – ‘a’ = 97, ‘b’ = 98, … • This character code is a widely-used code called ASCII – Next slide gives part of the ASCII code (the whole thing goes from 0 to 255, next slide shows only 32 to 126) 6 32 [space] 56 8 80 P 104 h 33 ! 57 9 81 Q 105 i 34 " 58 : 82 R 106 j 35 # 59 ; 83 S 107 k 36 $ 60 < 84 T 108 l 37 % 61 = 85 U 109 m 38 & 62 > 86 V 110 n 39 ' 63 ? 87 W 111 o 40 ( 64 @ 88 X 112 p 41 ) 65 A 89 Y 113 q 42 * 66 B 90 Z 114 r 43 + 67 C 91 [ 115 s 44 , 68 D 92 \ 116 t 45 - 69 E 93 ] 117 u 46 . 70 F 94 ^ 118 v 47 / 71 G 95 _ 119 w 48 0 72 H 96 ` 120 x 49 1 73 I 97 a 121 y 50 2 74 J 98 b 122 z 51 3 75 K 99 c 123 { 52 4 76 L 100 d 124 | 53 5 77 M 101 e 125 } 54 6 78 N 102 f 126 ~ 7 55 7 79 O 103 g ASCII code (excerpt from 32 to 126) Which of the following is the result of running this code: >> foo = 'I can't wait to learn more about MATLAB!' + 1 a) The value of the variable foo will be set to equal the string, and it will print foo = 'I can't wait to learn more about MATLAB!' b) Several numbers will be printed (one for each character in the string) c) MATLAB will give an error 8 Text messages from inside the belly of the beast—just like real text messages with their LOLZ, BRB, IKWYM, IDK, TTYL, program errors require some clues to interpret. INTERPRETING MATLAB ERRORS 9 Which of these shows the outcome when “factor(16+) + 3” is typed? >> factor(16+) + 3 (a) ??? factor(16+) + 3 Error: Unbalanced or unexpected parenthesis or bracket. >> factor(16+) + 3 ??? factor(16+) + 3 (b) | Error: Unbalanced or unexpected parenthesis or bracket. >> factor(16+) + 3 ??? factor(16+) + 3 (c) | Error: Unbalanced or unexpected parenthesis or bracket. 10 What does the 24 refer to in this error message? >> factor(48.001) ??? Error using ==> factor at 24 N must be a nonnegative integer. a) It doesn’t refer to anything—just jibberish b) 48.001 is the argument, and 2 * 24 is 48, so it’s a number you encounter when factoring 48 c) It is a location in the code for the factor function d) None or more than one of the above 11