6. Script file or m file
In this lesson you will learn:
- Creating and running script file or m file.
- Things to remind when using script file.
- Debugging Script file.
- How to locate program error.
- Programming style.
- Steps for developing a computer solution.
Till now we are working in MATLAB command window. In previous lecture you learn how to enter commands. Now this lecture shows you how to save your entire work in a script or M – file. You can perform operations in MATLAB in two ways:
- In the interactive mode, in which all commands are entered directly in the Command window.
- 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:
- 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.
- 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.
1. Creating and running script file or m file:
To open script file or M-file you have to follow the following steps:
- Open MATLAB.
- Click on script button at left most top side.
- A new file is open this is script or m-file where you can work and save your programs. In this file type the code, save file then compile it. The answer will show on command window.
Watch the video
2. Things to remind when using script file:
- The name of a script file must begin with a letter, and may include digits and the underscore character, up to 31 characters.
- Do not give a script file the same name as a variable.
- 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.
3. Debugging script file:
Program errors usually fall into one of the following categories.
- 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.
- 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.
4. How to locate program error:
- Test your program with a simple version of the problem which can be checked by hand.
- Display any intermediate calculations by removing semicolons at the end of statements.
5. Programming style:
- The name of the program and any key words in the first line.
- The date created, and the creators’ names in the second line.
- 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!
- The name of every user-defined function called by the program.
- Include input data and/or the input functions and comments for documentation.
- This section might contain functions for displaying the output on the screen.
6. Steps for developing a computer solution:
- State the problem concisely.
- Specify the data to be used by the program. This is the “input.”
- Specify the information to be generated by the program. This is the “output.”
- Work through the solution steps by hand or with a calculator; use a simpler set of data if necessary.
- Write and run the program.
- Check the output of the program with your hand solution.
This is the end of our sixth lesson. Please share this article if you like and do not forget to comment.