Presentation on theme: "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."— Presentation transcript:

VARIABLE NAMES A number by any other name wouldn’t smell as sweet? 2

Which of these is NOT a legal variable name in MATLAB? a)_factors_of_48_ b)my_factors c)factor d)f e)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

STRINGS IN MATLAB Computer scientists call words/text/sentences/punctuation “strings” 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

7 32[space]56880P104h 33!57981Q105i 34"58:82R106j 35#59;83S107k 36\$60<84T108l 37%61=85U109m 38&62>86V110n 39'63?87W111o 40(64@88X112p 41)65A89Y113q 42*66B90Z114r 43+67C91[115s 44,68D92\116t 45-69E93]117u 46.70F94^118v 47/71G95_119w 48072H96`120x 49173I97a121y 50274J98b122z 51375K99c123{ 52476L100d124| 53577M101e125} 54678N102f126~ 55779O103g 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

INTERPRETING MATLAB ERRORS 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. 9

Which of these shows the outcome when “factor(16+) + 3” is typed? >> factor(16+) + 3 ??? factor(16+) + 3 Error: Unbalanced or unexpected parenthesis or bracket. >> factor(16+) + 3 ??? factor(16+) + 3 | Error: Unbalanced or unexpected parenthesis or bracket. >> factor(16+) + 3 ??? factor(16+) + 3 | Error: Unbalanced or unexpected parenthesis or bracket. 10 (a) (b) (c)

What does the 24 refer to in this error message? >> factor(48.001) ??? Error using ==> factor at 24 N must be a nonnegative integer. 11 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