• No results found

For simple problems, entering commands at theMatlab prompt in the Command window is simple and efficient. However, when the number of commands increases, or you want to change the value of one or more variables, reevaluate a number of commands, typing at theMatlab becomes tedious.

You will find that for most uses ofMatlab, you will want to prepare a script, which is a sequence of commands written to a file. Then, by simply typing the script file name at aMatlab prompt, each command in the script file is executed as if it were entered at the prompt.

For more information, type help script.

Script File: Group of Matlab commands placed in a text file with a text editor. Matlab can open and execute the commands exactly as if they were entered at theMatlab prompt. The term

“script” indicates thatMatlab reads from the “script” found in the file. Also called “M-files,” as the filenames must end with the extension ‘.m’, e.g. example1.m.

M-files are text files and may be created and modified with any text editor. You need to know how to open, edit, and save a file with the text editor you are using. On a PC or Macintosh, an M-file editor may be brought up by choosing New from the File menu in theMatlab Command window and selecting M-file.

The script M-file is executed by choosing Run Script... from the File menu on a PC or Macintosh, or simply typing the name of the script file at the prompt in the Matlab command window.

Example 3.2 Quadratic root finding script

Create the file named qroots.m in your present working directory using a text editor:

% qroots: Quadratic root finding script format compact;

a=1 b=5 c=6

x = -b/(2*a);

y = sqrt(b^2-4*a*c)/(2*a);

s1 = x+y s2 = x-y

To execute the script M-file, simply type the name of the script file qroots at theMatlab prompt:

>> qroots a =

1 b =

5 c =

6 s1 =

-2 s2 =

-3 Comments:

• % qroots: % allows a comment to be added

• format compact: Suppresses the extra lines in the output display

• a=1, b=5, c=6: Sets values of a, b, c, will display result

• x & y: Semicolon at end of command means these intermediate values won’t be displayed

• s1, s2: Compute and display roots

Search rules for qroots command:

1. Display current Matlab variable qroots if defined 2. Execute built-in Matlab command qroots if it exists

3. Execute qroots.m if it can be found in the Matlab search path (described below)

Commands within the M-file have access to all variables in theMatlab workspace and all variables created by the M-file become part of the workspace.

Commands themselves are not normally displayed as they are evaluated. Placing the echo on command in the M-file will cause commands to be displayed. The command echo off (the default) turns off command display and echo by itself toggles the command echo state.

You could repeatedly edit qroots.m, change the values of a, b, and c, save the file, then have Matlab execute the revised script to compute a new pair of roots.

Matlab functions useful in M-files:

Command Description

disp(ans) Display results without identifying variable names echo [on|off] Control Command window echoing of script commands

input(’prompt’) Prompt user with text in quotes, accept input until “Enter” is typed keyboard Give control to keyboard temporarily. Type Return to return control

to the executing script M-file.

pause Pause until user presses any keyboard key pause(n) Pause for n seconds

waitforbuttonpress Pause until user presses mouse button or keyboard key

The input command is used for user input of data in a script and it is used in the form of an assignment statement. For example:

a = input(’Enter quadratic coefficient a: ’);

When this command is executed, the text string Enter quadratic coefficient a: is displayed as a user prompt in the command window. The user then types in data value, which is assigned to the variable a. Because this input command ends with a semicolon, the entered value of a is not displayed when the command is completed.

Example 3.3 Revised quadratic roots script

Change script for computing quadratic roots by:

1. Prompting for input of coefficients a, b, and c;

2. Format the display of the computed roots s1 and s2;

Script rqroots.m:

% rqroots: Revised quadratic root finding script format compact;

% prompt for coefficient input

a = input(’Enter quadratic coefficient a: ’);

b = input(’Enter quadratic coefficient b: ’);

c = input(’Enter quadratic coefficient c: ’);

disp(’’)

% compute intermediate values x & y x = -b/(2*a);

y = sqrt(b^2-4*a*c)/(2*a);

% compute and display roots s1 = x+y;

disp(’Value of first quadratic root: ’),disp(s1);

s2 = x-y;

disp(’Value of second quadratic root: ’),disp(s2);

Two examples of the results from this script:

>> rqroots

Enter quadratic coefficient a: 1 Enter quadratic coefficient b: 5 Enter quadratic coefficient c: 6 Value of first quadratic root:

-2

Value of second quadratic root:

-3

>> rqroots

Enter quadratic coefficient a: 1 Enter quadratic coefficient b: 4 Enter quadratic coefficient c: 8 Value of first quadratic root:

-2.0000+ 2.0000i

Value of second quadratic root:

-2.0000- 2.0000i

M-file Commands Command Description

edit test Open test.m for editing using the built-inMatlab text editor, same as Open... in File menu

Effective Use of Script Files

The following are some suggestions on the effective use of Matlab scripts:

1. The name of a script file must follow theMatlab convention for naming variables; that is, the name must begin with a letter and may include digits and the underscore character.

2. Do not give a script file the same name as a variable it computes, becauseMatlab will not be able to execute that script file more than once unless the variable is cleared. Recall that typing a variable name at the command prompt causesMatlab to display the value of that variable. If there is no variable by that name, thenMatlab searches for a script file having that name. For example, if the variable rqroot was created in a script file having the name rqroot.m, then after the script is executed the first time, the variable rqroot exists in the Matlab workspace. If the script file is modified and an attempt is made to run it a second time, Matlab will display the value of rqroot and will not execute the script file.

3. Do not give a script file the same name as aMatlab command or function. You can check to see whether a function already exists by using the the which command. For example, to see whether rqroot already exists, type which rqroot. If it doesn’t exist,Matlab will display rqroot not found. If it does exist,Matlab will display the full path to the function. For more details as to the existence of a variable, script, or function having the name rqroot, type exist(’rqroot’). This command returns one of the following values:

0 if rqroot does not exist

1 if rqroot is a variable in the workspace

2 if rqroot is an M-file or a file of unknown type in the Matlab search path 3 if rqroot is a MEX-file in the Matlab search path

4 if rqroot is a MDL-file in the Matlab search path 5 if rqroot is a built-inMatlab function

6 if rqroot is a P-file in theMatlab search path 7 if rqroot is a directory

4. As in interactive mode, all variables created by a script file are defined as variables in the workspace. After script execution, you can type who or whos to display information about the names, data types and sizes of these variables.

5. You can use the type command to display an M-file without opening it with a text editor.

For example, to view the file rqroot.m, the command is type rqroot.