220 likes | 317 Views
Scientific Programming Using MATLAB, Fall 2011-2012. TIRGUL #3: Strings, Flow control 1. Characters & Strings. Character = a printable/readable sign Creation in MATLAB: ‘a letter’ Examples: 'r' or '.' or ' ‘ String = an array of characters Creation in MATLAB: 'a few letters‘
E N D
Scientific Programming Using MATLAB, Fall 2011-2012 TIRGUL #3: Strings, Flow control 1
Characters & Strings • Character = a printable/readable sign • Creation in MATLAB: ‘a letter’ • Examples: 'r' or '.' or ' ‘ • String = an array of characters • Creation in MATLAB: 'a few letters‘ • Example: A = 'this is a MATLAB string'
String and number conversions • num2str: converts numbers to strings • myStr = num2str(3.2) myStr = ‘3.2’ • str2num: converts strings to numbers
Strings - concatenation • We can compose complex strings using concatenation • Combining numbers in strings • Example: • numStudents = 32; • [‘The number of students is ’, num2str(numStudents)]
Other string manipulations • length(myString) • strcmp(string1, string2) • strcmpi – case insensitive • ‘help strfun’ – list of all string-related functions • Example – Tirgul2Strings.m
Flow control • Conditional statements tell a program what to do when certain events occur • Conditioned • if • switch • Iterative (next week) • for • while
Logical operators • Logical Expression – an expression that has a value TRUE or FALSE (1 or 0) • Comparisons: • (x == 5) • (x > 3) • (x <= 6) • Special operators: • ~ not • && and • || or
Assignment vs. assessment • For assignment, use = • myVar = 3; I defined a variable, myVar, and assigned to it the value 3. • For assessment, use = = • newVar = myVar = = 3 newVar = 1 I asked whether myVar equals 1. MATLAB checked and assigned the value 1 (meaning “yes”) to newVar .
If… else • General form: Ifcondition statements else statements end
If… else • Example: • if (rem(x,2) == 1) disp(‘Odd’); else disp(‘Even’); end
If… elseif, elseif, else • Example: • if (rem(x,2) == 1) disp(‘Odd’); elseif (x == 0) disp(‘Zero’); else disp(‘Even’); end
Examples: • ifElseScript • ifNestedScript
Switch statements • Switch among several cases based on value of expression • Good for discrete numbers of possibilities • Equivalent to a series of if/elseif/else’s • Example: switchvalue_of_variable case 1 disp(‘variable equals 1’); case 2 disp(‘variable equals 2’); otherwise disp(‘not in the list’); end
Examples: • switchScript • (ifArraysScript)
Indentation • Used to make the code more reader-friendly • Don’t work hard, just mark the lines and use Ctrl-I • Example: IfIndent.m
Flow control – iterative execution • ‘for’ loop • ‘while’ loop
‘for’ loops • for index = start : [increment :] end statements end • Increment • Default = 1 • Can be positive / negative • Loop stops when index exceeds ‘end’ value • Loops can be nested.
A word about indexing style ~~~~~ for i = 1:100 for j = 1:20 … end end ~~~~~ ~~~~~ num_neurons = 100 num_trials = 20 for i_neuron = 1:num_neurons for i_trial = 1:num_trials … end end ~~~~~ Not good for ii = 1:100 for jj = 1:20 … end end ~~~~~ Best Better
‘while’ loop • while expression statements end • MATLAB evaluates expression as logical “true” or “false” • “false” = zero • “true” = non-zero number • Make sure the expression changes to ‘false’ at some point when executing the statements – otherwise there will be an infinite loop • This WILL happen to you - to stop MATLAB from executing CTRL-C
For vs. While • While: • Executing commands until a condition is met • For: • There is a known number of iterations • Can loop over, for example: • Variable indices • Web addresses • Files to be saved • Plots to display
Examples: forAndWhile.m • Take home messages: • Correct indexing • Variable initialization • ‘find’ can replace ‘for’ • ‘counter’ variables • When to use ‘for’ vs. ‘while’ • Indentation
Practice • Create a variable that holds the string “learning matlab is fun, fun, fun”. • Create another variable that holds all the characters from a to z (lower-case) • Write a loop that checks for each English letter, how many times it appears in the sentence above (hint: find the right function using “help strfun”). • How many characters appear in the sentence? Check if there are more than 10, if so print a message that displays this number.