EGR 106 Lecture 5 Editor, Scripts, Input/Output - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

EGR 106 Lecture 5 Editor, Scripts, Input/Output

Description:

Opens Editor Window to Create New M-File. Opens an Existing File in Editor Window to Edit & Save ... Equations Ax=b. disp('Solution of System of Equations Ax=b ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 20
Provided by: mciseU
Category:

less

Transcript and Presenter's Notes

Title: EGR 106 Lecture 5 Editor, Scripts, Input/Output


1
EGR 106 Lecture 5 Editor, Scripts, Input/Output
  • Review of Script Files Editor
  • Input and Output Commands
  • Error Messages Debugging
  • Applications
  • Textbook 4.1-4.5

2
Review of Script (m-file) Concept
  • A file containing MATLAB commands
  • Can be re-executed
  • Is easily changed/modified or e-mailed to someone
  • Commands are executed one by one, sequentially
  • File is executed by typing its name (without .m)
  • Results appear in the command window (or use )
  • Can be created using any text editor
  • .m extension
  • Listed in Current Directory window

3
  • MATLABs Built-in, Color Editor
  • Can create a new file
  • or open an existing
  • M file (icons or click
  • on file name)
  • Color used to aid in
  • file creation
  • (command types,
  • typos, etc.)

Opens Editor Window to Create New M-File
Opens an Existing File in Editor Window to Edit
Save
4
Typical Example of Script File in Editor
Window
  • typical Windows menu
  • line numbers
  • run button or F5
  • debug capability


comment lines begin with percent sign
note use of semicolons to suppress screen output
of unimportant variables
note automatic change of colors to help programmer
5
  • How scripts get data
  • From arrays in the current workspace
  • load file_name
  • From arrays defined in the script or Command
    Window
  • Using the input command
  • Numeric
  • x input(' how many? ')
  • String
  • x input(' name? ', 's')

6
  • How scripts show or output data
  • Use of array name command
  • Use of display command
  • Existing array (a single array only if
    necessary, use !!)
  • disp(x) or disp(x,y)
  • Text
  • disp(' The task is done ')
  • Use of fprintf command
  • Save command (creates .mat file)
  • save file_name

7
Matlab Data Files (not in the text)
  • Types
  • .asv auto save
  • ascii regular text files
  • .mat Matlabs proprietary format (multiple
    variables)

8
  • Example
  • Note that disp shortens the resulting output
    by dropping the array name and removing blank
    lines

9
Converting Numerical to String Variablesfor
Displaying (not in text)Sometimes we wish to
display a numerical variable that has been used
or computed within a MATLAB code along with
particular text. We can use the command
num2str(x) to convert the number x to a
string variable for display. This command can
also be used for multiple display in a single
line. Example
Num2Str Example clcclear all for
n15 xn2 disp('n ',num2str(n), x
',num2str(x)) end
n 1 x 1 n 2 x 4 n 3 x 9 n 4 x
16 n 5 x 25
10
fprintf Command
  • Allows intermixing of text and numerical
    display
  • Output can be formatted to produce good looking
    results
  • Command has many options which can be
    challenging

fprintf('\n Example Using fprintf
Command') fprintf('\n Print Out Formatted Square
Root Results') for n15 xsqrt(n) fprintf('\n n
i sqrt(n) 5.2f',n,x) end
Example Using fprintf Command Print Out
Formatted Square Root Results n 1 sqrt(n)
1.00 n 2 sqrt(n) 1.41 n 3 sqrt(n)
1.73 n 4 sqrt(n) 2.00 n 5 sqrt(n) 2.24
See Text and Help Menu for more details
11
  • Other Useful Script Commands
  • clc clears the command window
  • clf clears current figure window
  • clear clears all variables from memory
  • pause stops operation and waits for a key press
  • pause(n) stops operation and waits for n
    seconds
  • to continue a long line of code use three periods
    at end (), hit enter and continue typing the
    remainder of text on next line
  • - typing Ctrl c in command window will break
    and end a current computation

12
MATLAB Workspace
  • MATLAB workspace contains the variables that have
    been defined and stored during a session
  • Such variables include those defined either in
    the Command Window or in a script file (they
    share the same memory)
  • These variables can then be reused or redefined
    as needed as this generally helps in the
    programming
  • Later we will see that function files do not
    operate in this manner and variables within such
    files will normally not be shared with other
    parts of programs

13
Example
Results in Command Window from Running the File
14
Errors in Your Scripts
  • Syntax errors

Note red text bad news
But tells you where
15
  • Run-time errors inf or NaN results

Note black text okay, just a warning
16
  • Logical errors in your program hard to find
  • Example quadratic equation solver
  • But x22x1 (x1)2 ? x 1
  • Use the built-in debugger

Whats Wrong ?
17
Developing and Debugging Code
  • As you develop longer MATLAB code, you may want
    to avoid running the beginning portions that you
    already know are working properly. This can be
    done by temporarily covering these portions with
    a sign thus turning them into non-executable
    code. Once everything is working OK, these extra
    signs can then be removed.
  • Using the marking on other portions of code
    can also be useful to eliminate certain parts
    thereby focusing on just the remain portions of
    code.
  • Sometimes it can be useful to add some temporary
    print out statements in key portions of your
    code where problems are occurring. This way you
    can see more details on the intermediate
    computations and hopefully trace and solve the
    problem.
  • MATLAB also has more sophisticated built-in
    debugging tools that will allow you to control
    and see intermediate steps.

18
Symbol Can be Used to Block Out Parts of a
Program to Debug Your Codes
Lecture 4 Examples clc Matrix
Multiplication A1,2,0,11,2,2,10,1,3,12,1,4,0
b2,1,2,0' disp('Ab ') disp(Ab) pauseclc A
B1,2,3,41,1,1,11,2,1,22,3,2,3 disp('AB
') disp(AB) disp('BA ') disp(BA) pauseclc
Find Inverses A disp('A-1 ')
disp(inv(A)) pause,clc A(1,)0A
disp('A-1 ') disp(inv(A)) pauseclc
Solution of System of Equations Axb
disp('Solution of System of Equations Axb\n')
A1,2,0,1,21,2,2,1,30,1,3,1,42,1,4,0,01,2,2,1
,0 b2,1,2,0,1' xinv(A)b
Lecture 4 Examples clc Matrix
Multiplication A1,2,0,11,2,2,10,1,3,12,1,4,0
b2,1,2,0' disp('Ab ') disp(Ab) pauseclc A
B1,2,3,41,1,1,11,2,1,22,3,2,3 disp('AB
') disp(AB) disp('BA ') disp(BA) pauseclc
Find Inverses A disp('A-1 ') disp(inv(A)) pause
,clc A(1,)0A disp('A-1 ') disp(inv(A)) pause
clc Solution of System of Equations
Axb disp('Solution of System of Equations
Axb\n') A1,2,0,1,21,2,2,1,30,1,3,1,42,1,4,0,
01,2,2,1,0 b2,1,2,0,1' xinv(A)b
Portion of Code Commented Out
19
Typical Script or M-File Structure
EGR 106 Lecture 6 Examples M. Sadd
Programming, Relational/Logical
Operators clc,clear all Scalar
Operations disp('Scalar Relational
Operators') a5lt8 b5gt10 cab pause clc
Vector/Matrix Operations disp('Vector Relational
Operators') x1,2,3 y2,1,1 zxgty zzxy zzz
xlt2 pause clc disp('Matrix Relational
Operators') A1,23,4 B3,45,6 CAltB CCBltA
CCCAlt2 pause clc Logical Operators AND
Operator AB disp('Logical AND Operator') a37 b
30 . . .
Always start with title/heading/name
Wise to always clear window, figures variables
Good to provide subheading titles or comments at
several places within file to explain what is
going on
Be careful not to lose your work! Continuously
save. At the end of the development process ,
always save your files using a name that makes
sense and connections with its purpose
Write a Comment
User Comments (0)
About PowerShow.com