Script files or M - files


>> Till now we are working in MATLAB command window. In previous lecture you learn how to enter command. Now this lecture show you how to save your entire work in a script or M - file.


>> You can perform operations in MATLAB in two ways: 

1. In the interactive mode, in which all commands are entered directly in the Command window.

2. By running a MATLAB program stored in script file. This type of file contains MATLAB commands, so running it is equivalent to typing all the commands—one at a time—at the Command window prompt. You can run the file by typing its name at the Command window prompt.

>> MATLAB allows writing two kinds of program files:

1. Scripts or M-file - script files or M-file are program files with .m extension. In these files, you write series of commands, which you want to execute together. Scripts do not accept inputs and do not return any outputs. They operate on data in the workspace.

2. Functions - functions files are also program files with .m extension. Functions can accept inputs and return outputs. Internal variables are local to the function.

You can use the MATLAB Editor or any other text editor to create your .m files.

Creating and Running Script File or M-file:

To open script file or M-file you have to follow the following steps:
1. Open MATLAB.
2. Click on file at left most top side.


3. Click on new and then on script or m-file


4. A new file is open this is script or m-file where you can work and save your programms. In this file type the code to be execute, save file then compile it. The answer will show on command window.


Keep in mind when using script files:

1. The name of a script file must begin with a letter, and may include digits and the underscore character, up to 31 characters.


2. Do not give a script file the same name as a variable.


3. Do not give a script file the same name as a MATLAB command or function. You can check to see if a command, function or file name already exists by using the exist command.

Debugging Script Files:

Program errors usually fall into one of the following categories.

1. Syntax errors such as omitting a parenthesis or comma, or spelling a command name incorrectly. MATLAB usually detects the more obvious errors and displays a message describing the error and its location.



2. Errors due to an incorrect mathematical procedure, called runtime errors. Their occurrence often depends on the particular input data. A common example is division by zero.

To locate program errors, try the following:

1. Test your program with a simple version of the problem which can be checked by hand.



2. Display any intermediate calculations by removing semicolons at the end of statements.

Programming Style:

>> Comments section
a. The name of the program and any key words in the first line.
b. The date created, and the creators' names in the second line.
c. The definitions of the variable names for every input and output variable. Include definitions of variables used in the calculations and units of measurement for all input and all output variables!

d. The name of every user-defined function called by the program.

>> Input section Include input data and/or the input functions and comments for documentation.

>> Calculation section



>> Output section This section might contain functions for displaying the output on the screen.

Steps for developing a computer solution:

1. State the problem concisely. 

2. Specify the data to be used by the program. This is the “input.” 


3. Specify the information to be generated by the program. This is the “output.” 


4. Work through the solution steps by hand or with a calculator; use a simpler set of data if necessary. 


5. Write and run the program. 


6. Check the output of the program with your hand solution.