MATLAB M-Files

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 Scripts: Scripts do not accept input arguments or return output arguments. They operate on data in the workspace.

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.

M-File Scripts

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 MATLAB M-Files 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 MATLAB M-Files which is placed along with the new script icon.

Use Ctrl + O keyboard shortcut or open file icon MATLAB M-Files 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.

MATLAB M-Files

Now add some code inside the script.

MATLAB M-Files

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

  • Use the edit command to create a script at the command line.
  • Syntax:
MATLAB M-Files
  • The edit command is used for opening or editing an existing script and to create a new script.
  • When the edit command is entered with the filename, and if the file doesn't exist, then it prompts to create a new one. Click yes to create the new script.
MATLAB M-Files
  • The script file name must start with a letter and contain only letters, numbers, or underscores; otherwise, it shows an error. And if you save it without the recommended file name, then you will not be able to run this file unless you change its name.
MATLAB M-Files
  • If you enter the edit command without a file name, then a blank script with a default file name will be created in the current folder. When you save the script, it prompts to change the default file name and current folder.
  • It is a good practice to add comments in your code as and when required because comments better describe the code and provide valuable hints to the viewer.

Example:

Create the following script:

MATLAB M-Files

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:

MATLAB M-Files

Live Scripts in MATLAB

  • These are also the program files as a .m extension, but these are saved with the .mlx extension.
  • The main difference between a normal script and the live script is that with the live script, we can interact with the code and the output simultaneously in a single window.
  • As we write comments in scripts with the % symbol, here, we can use text formatting options for writing code and comments without using the percent symbol.
MATLAB M-Files
  • We can convert the normal script file into a live script file by using Save As option and save the file with .mlx extension.
  • While saving the normal script file as a live script file, it's better to keep the live script file with a different name; because if both files have the same name, then the live script will run.
  • To run a live script file, enter the file name on the command line.
  • Or use Run icon in the MATLAB editor.
  • If we use the command line to run a live script, it opens a new window for the output, while by using the Run icon in the MATLAB editor, we can view the output in a sidebar.
MATLAB M-Files
  • We can create live scripts using New Live Script icon MATLAB M-Files on the Home tab of the MATLAB environment.
  • Or use command edit <.mlx extension>.
MATLAB M-Files