MATLAB is a programming language, as well as an interactive computational environment. Files that include code in the MATLAB language are called M-files.
M-files are ordinary ASCII text data written in MATLAB's language. Such files are called "M-files" because they must have the filename extension ".m" at the end of their name (like myfunction.m). This extension is needed for this data to be interpreted by MATLAB.
M files can be created using any editor or word processing function.
Types of M-Files
There are two types of M-Files:
M-File Functions: Functions can accept input arguments and return output arguments. Internal variables are local to the function.
We can use the MATLAB editor or any other text editor to create our .mfiles. In this topic, we will discuss the script files.
A script file is an external file that includes a sequence of MATLAB statements. Script files have a filename extension .m and are known as M-files. M-files can be scripts that execute a list of MATLAB statements, or they can be functions that can accept arguments and can produce one or more outputs.
Creating Scripts in MATLAB
We can create a script in two ways in MATLAB:
First is by using the MATLAB environment/ MATLAB editor.
And second is by using the command window.
Create Script using MATLAB editor
Use New Script icon on the Home tab of the environment.
Use Ctrl + n keyboard shortcut to get new script file.
On clicking the new script icon or using the keyboard shortcut, a blank untitled file gets opened.
The blank file has a default directory to save it. We can change the file storage location as per our requirement.
Save the script with the keyboard shortcut Ctrl + s or use the Save icon which is placed along with the new script icon.
Use Ctrl + O keyboard shortcut or open file icon to open an already existing script.
There are three more menu tabs available to create a script as, Editor, Publish, and View.
We can work with multiple scripts in new tabs.
The Command Window area is divided into two panes, one for Script editor and another one for Command Window.
We can simultaneously work with script and command line.
Now add some code inside the script.
The code is the same as we type at the command line.
Use the semicolon at the end of each line that doesn't output anything.
If you want to show any line as the output while running the script, don't use semicolon at the end of that line.
Create Script using Command Window of MATLAB
Create the following script:
Save it with the name sphere_plot.
Now enter the file name sphere_plot on the command line.
It outputs the following result in a new window:
Live Scripts in MATLAB
Next TopicMATLAB Functions