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.
Script M-Files Example : 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 the Matlab prompt: >> qroots a = 1 b = 5 c = 43 6 s1 = -2 s2 = -3
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 computes 3. 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 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 ….
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.
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.