EGR 106 Lecture 5 Editor, Scripts, InputOutput - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

EGR 106 Lecture 5 Editor, Scripts, InputOutput

Description:

Sometimes we wish to display a numerical variable that has been used or computed ... Wise to always clear window, figures & variables ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 16
Provided by: mce9
Category:

less

Transcript and Presenter's Notes

Title: EGR 106 Lecture 5 Editor, Scripts, InputOutput


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
(No Transcript)
4
  • 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
5
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
6
  • How scripts get data
  • From arrays in the current workspace
  • load file_name
  • From arrays defined in the script
  • Using the input command
  • Numeric
  • x input(' how many? ')
  • String
  • x input(' name? ', 's')

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

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

11
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

12
Example
Results in Command Window from Running the File
13
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.

14
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
15
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