An m-file, or script file, is a simple text file where you can place MATLAB commands. When the file is run, MATLAB reads the commands and executes them exactly as it would if you had typed each command sequentially at the MATLAB prompt. All m-file names must end with the extension '.m' (e.g. plot.m). If you create a new m-file with the same name as an existing m-file, MATLAB will choose the one which appears first in the path order (help path for more information). To make life easier, choose a name for your m-file which doesn't already exist. To see if a filename.m exists, type help filename at the MATLAB prompt.
For simple problems, entering your requests at the MATLAB prompt is fast and efficient. However, as the number of commands increases or trial and error is done by changing certain variab or values, typing the commands over and over at the MATLAB prompt becomes tedious. M-files will be helpful and almost necessary in these cases.
If you are using PC or Mac:
To save the m-file, simply go to the File menu and choose Save (remember to save it with the '.m' extension). To open an existing m-file, go to the File menu and choose Open .
After the m-file is saved with the name filename.m in the MATLAB folder or directory, you can execute the commands in the m-file by simply typing filename at the MATLAB prompt.
If you don't want to run the whole m-file, you can just copy the part of m-file that you want to run and paste it at the MATLAB prompt.
Use your browser's "Back" button to return to the previous page.