Matlab m Files Tutorial
Matlab m Files Tutorial
i
MATLAB M-Files
Audience
This tutorial has been prepared for the beginners to help them understand basic to
advanced functionality of MATLAB M-File. After completing this tutorial you will find
yourself at a moderate level of expertise in using M-files from where you can take yourself
to next levels.
Prerequisites
We assume you have a little knowledge of any computer programming and understand
concepts like variables, constants, expression, statements, etc. If you have done
programming in any other high-level programming language like C, C++ or Java, then it
will be very much beneficial and learning MATLAB M-File will be like a fun for you.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected]
i
MATLAB M-Files
Table of Contents
About the Tutorial ............................................................................................................................................ i
Audience ........................................................................................................................................................... i
Prerequisites ..................................................................................................................................................... i
ii
1. MATLAB M-File — Introduction MATLAB M-Files
MATLAB allows you to write a series of commands into a file and execute the file as a
complete unit, like writing a function and calling it. It is done using M-file.
M-File also called as the script file is a series of MATLAB commands that will get executed
sequentially when the file is executed.
Program Files
MATLAB allows writing two kinds of program files, which are as follows:
Scripts
Script files are program files with .m extension. In these files, you write a 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
Function files are also program files with .m extension. Functions can accept inputs and
return outputs. Internal variables are local to the function.
1
MATLAB M-Files
Let us understand how to create and run the m-file in the next chapters.
2
2. MATLAB M-File — Create and Save MATLAB M-Files
In this chapter, we will learn how to create and save a M-file. Let us begin by understanding
about creating a M-file.
Create M-file
To create m-file, we will make use of MATLAB IDE as shown below. Here IDE refers to an
integrated development environment.
3
MATLAB M-Files
Save M-file
You will get an untitled file. Let us save the file as firstmfile.m.
Click on the save button and it will open a popup, where you can enter the name of the
file.
4
MATLAB M-Files
Now, you are free to write your commands in the file below:
5
3. MATLAB M-File — Run MATLAB M-Files
In the previous chapter, we have seen how to create and save a m-file. Now in this chapter,
we will write a simple code and run the same.
a = 5; b = 7;
c = a + b
d = c + sin(b)
e = 5 * d
f = exp(-d)
Click on the Run button as highlighted above to see the result inside the command window
as shown below:
6
MATLAB M-Files
7
4. MATLAB M-File — Functions MATLAB M-Files
Functions operate on variables within their own workspace, which is also called the local
workspace. These functions separate the variables from the workspace which you access
at the MATLAB command prompt. This is called the base workspace.
Functions can accept more than one input arguments and may return more than one
output arguments.
Here out1, out2...outN are output variables. It can be a single variable or comma
separated. The variables in1, in2, in3...inN are input variables which can be single variable
or comma separated ones. The function in MATLAB starts with the keyword function as
shown in the syntax.
While naming your m-file, you should take care that the file name and the function name
has to match. You cannot use the name of any built-in function available in MATLAB.
Let us now create a simple function and save it as .m file and run it. In MATLAB, the IDE
allows you to select what type of file you want to create as shown below:
8
MATLAB M-Files
Click on the Function, and it will open a new file as shown below:
Now you can update the output variables, the function name and the input variables in
the above untitled file and save the file with the same name as the function name.
The name of our function is MaxNumber() and it gives the maximum number from the
input value passed.
Now let us run the function to get the output. You can call the function by using
MaxNumber(100,50).
9
MATLAB M-Files
10
5. MATLAB M-File — Import Data MATLAB M-Files
In this chapter, we will understand how to import data from an existing m-file in MATLAB.
Consider the m-file firstmfile.m. The contents of the file are as follows:
a = 5; b = 7;
c = a + b
d = c + sin(b)
e = 5 * d
f = exp(-d)
When you execute the file, the data variables are available in the workspace, as shown
below:
Let us clear the workspace and the command prompt. Hence, we can now import the file
and also check the data available.
11
MATLAB M-Files
Let us clean the command window by using clc command as shown below:
12
MATLAB M-Files
Now, type the m-file name as shown below. It will execute the code inside the file.
The workspace is loaded with all the variables that are used inside the file.
You can also check the details of the variables by using the whos command as shown
below:
13