18. Scripts Functions Data

18. Scripts Functions Data - ENGR 101, Section 100, Fall 10...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
ENGR 101, Section 100, Fall 10 M. Wellman 1 Scripts, Functions, Input/Output ENGR 101, Lecture 18: 17 Nov 10 Announcements Project 7 posted (due Wed 1 Dec) may require some constructs from next week, but you have enough Matlab to get a good start My office hours Tue and Fri 9:30–11:00 this week: Fri hrs changed to today 12:00–1:30
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ENGR 101, Section 100, Fall 10 M. Wellman 2 MATLAB Workspace “main” scope for MATLAB functions and variable names Names introduced (e.g., initial assignment) and referenced from command window or script file Lifetime of names: from introduction until clear command Use who command to display active variables in workspace Script Files A list of MATLAB commands, saved in a file Use extension .m hence called "M-files" can run scriptname.m by typing scriptname as command Running script file equivalent to typing in commands, in sequence variables in a script file have workspace scope
Background image of page 2
ENGR 101, Section 100, Fall 10 M. Wellman 3 disp Procedure Displays the value of an expression on the screen form: disp( expr ) Example: x = [5 : 9]; disp(x); Output: 5 6 7 8 9 display Procedure Like disp, but precedes output with associated variable and ' = ' form: display( expr ) Example: x = 59; display(x); display(59); Output: x = 59 ans = 59
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ENGR 101, Section 100, Fall 10 M. Wellman 4 Read/Eval/Display Loop The MATLAB interpreter processes input (from keyboard or scripts) as follows: 1. read the next statement a command, assignment, or expression 2. execute the statement, eval uating expressions as needed 3. if an assignment or expression stmt, call display on the result unless statement ends in semicolon ( ; ) Read Eval Display char Function converts ASCII codes (integer between 0 and 127) into characters applies elt-by-elt to an array Examples: char(65:70) char([65:70]+1) 3+'a' char(3+'a') ABCDEF BCDEFG 100 d
Background image of page 4
ENGR 101, Section 100, Fall 10 M. Wellman 5 Character Arithmetic letters = char([0:25]+'a'); units = char(mod([1:26],10)+'0'); tens = char(floor([1:26]/10)+'0'); disp([tens; units; letters]); 00000000011111111112222222 12345678901234567890123456 abcdefghijklmnopqrstuvwxyz Formatted Output The procedure fprintf provides fine control of how data is formatted for output to display or file General form for display output: fprintf( format_string , expr1 , expr2 , ) displays expression values as specified in the format string. Format string: literal string, with specifications to format each expression Example: fprintf('Average score: %-5.1f points.\n', avgScore) format string Format specifier expr1
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ENGR 101, Section 100, Fall 10 M. Wellman 6 Format Specifiers General form for format specifier: % { flag }{ width }{ . { precision
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

18. Scripts Functions Data - ENGR 101, Section 100, Fall 10...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online