1 / 22

Scientific Programming Using MATLAB, Fall 2011-2012

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‘

Download Presentation

Scientific Programming Using MATLAB, Fall 2011-2012

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scientific Programming Using MATLAB, Fall 2011-2012 TIRGUL #3: Strings, Flow control 1

  2. 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'

  3. String and number conversions • num2str: converts numbers to strings • myStr = num2str(3.2)  myStr = ‘3.2’ • str2num: converts strings to numbers

  4. Strings - concatenation • We can compose complex strings using concatenation • Combining numbers in strings • Example: • numStudents = 32; • [‘The number of students is ’, num2str(numStudents)]

  5. Other string manipulations • length(myString) • strcmp(string1, string2) • strcmpi – case insensitive • ‘help strfun’ – list of all string-related functions • Example – Tirgul2Strings.m

  6. Flow control • Conditional statements tell a program what to do when certain events occur • Conditioned • if • switch • Iterative (next week) • for • while

  7. 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

  8. 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 .

  9. If… else • General form: Ifcondition statements else statements end

  10. If… else • Example: • if (rem(x,2) == 1) disp(‘Odd’); else disp(‘Even’); end

  11. If… elseif, elseif, else • Example: • if (rem(x,2) == 1) disp(‘Odd’); elseif (x == 0) disp(‘Zero’); else disp(‘Even’); end

  12. Examples: • ifElseScript • ifNestedScript

  13. 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

  14. Examples: • switchScript • (ifArraysScript)

  15. Indentation • Used to make the code more reader-friendly • Don’t work hard, just mark the lines and use Ctrl-I • Example: IfIndent.m

  16. Flow control – iterative execution • ‘for’ loop • ‘while’ loop

  17. ‘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.

  18. 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

  19. ‘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

  20. 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

  21. Examples: forAndWhile.m • Take home messages: • Correct indexing • Variable initialization • ‘find’ can replace ‘for’ • ‘counter’ variables • When to use ‘for’ vs. ‘while’ • Indentation

  22. 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.

More Related