Script M-Files Group of Matlab commands placed in a text file with a text editor. Matlab can open...

5
Script M-Files •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 the Matlab prompt. •The term “script” indicates that Matlab 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.

Transcript of Script M-Files Group of Matlab commands placed in a text file with a text editor. Matlab can open...

Page 1: Script M-Files 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.

Script M-Files

•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 the Matlab prompt. •The term “script” indicates that Matlab 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.

Page 2: Script M-Files 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.

Script M-FilesExample : 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+ys2 = x-y

To execute the script M-file, simply type the name of the script file qroots at the Matlab prompt:>> qrootsa =1b =5c =436s1 =-2s2 =-3

Page 3: Script M-Files 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.

Effective Use of Script Files

1. 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 computes3. Do not give a script file the same name as a Matlab command or function. To check existence of command, type exist(’rqroot’). This command returns one of the following values:

0 if rqroot does not exist1 if rqroot is a variable in the workspace2 if rqroot is an M-file or a file of unknown type in

the Matlab search path….

Page 4: Script M-Files 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.

Effective Use of Script Files

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

Page 5: Script M-Files 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.

Matlab Search Path, Path Management

Matlab search path: Ordered list of directories that Matlab searches to find script and function M-files stored on disk.

Commands to manage this search path:matlabpath: Display search path.addpath dir: Add directory dir to beginning of matlabpath. If you create a directory to store your script and function M-files, you will want to add this directory to the search path.rmpath dir: Remove directory dir from the matlabpath.