MATLAB - M-Files
MATLAB allows to write series of commands into a file and
execute the file as complete unit, like writing a function and
calling it.
The M Files
MATLAB allows writing two kinds of program files −
Scripts − script files 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.
Creating and Running Script File
To create scripts files, you need to use a text editor. You can open
the MATLAB editor in two ways −
Using the command prompt
Using the IDE
If you are using the command prompt, type edit in the command
prompt. This will open the editor. You can directly type edit and
then the filename (with .m extension)
edit
Or
edit <filename>
The above command will create the file in default MATLAB
directory. If you want to store all program files in a specific folder,
then you will have to provide the entire path.
Let us create a folder named progs. Type the following commands
at the command prompt (>>) −
mkdir progs % create directory progs under default
directory
chdir progs % changing the current directory to
progs
edit prog1.m % creating an m file named prog1.m
After creating and saving the file, you can run it in two ways −
Clicking the Run button on the editor window or
Just typing the filename (without extension) in the command
prompt: >> prog1