English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MATLAB M-Files

So far, we have used the MATLAB environment as a calculator. However, MATLAB is also a powerful programming language and an interactive computing environment.

In the previous chapter, you learned how to enter commands from the MATLAB command prompt. MATLAB also allows you to write a series of commands into a file and execute the file as a complete unit, such as writing a function and calling it.

M files

MATLAB allows you to write two types of program files-

  • Script−Script files are program files with the .m extension. In these files, you write a series of commands to be executed together. Scripts do not accept input and do not return any output. They operate on the data in the workspace.

  • Function−Function files are program files with the .m extension. Functions can accept input and return output. Internal variables are the local variables of the function.

You can create it using the MATLAB editor or any other text editor.mFiles. In this section, we will discuss script files. Script files contain multiple lines of MATLAB commands and function calls. You can run a script by entering the script name in the command line.

Create and run script files

To create script files, you need to use a text editor. You can open the MATLAB editor in two ways-

  • Using command prompt

  • Using IDE

If using the command prompt, please entereditCommand prompt. This will open the editor. You can directly entereditEnter the filename (with the .m extension)

edit 
Or
edit <filename>

The above command will create a file in the default MATLAB directory. If you want to store all program files in a specific folder, you must provide the complete path.

Let's create a folder named progs. At the command prompt (>>), enter the following command-

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

If it is the first time you create a file, MATLAB will prompt you to confirm. Click Yes.

Or, if you are using an IDE, select “New”->“Script”. This will also open the editor and create an untitled file. You can name and save the file after entering the code.

Enter the following code in the editor-

NoOfStudents = 6000;
TeachingStaff = 150;
NonTeachingStaff = 20;
Total = NoOfStudents + TeachingStaff ...
   + NonTeachingStaff;
disp(Total);

After creating and saving the file, it can be run in two ways-

  • ClickRunThe button on the editor window or

  • Type the filename (without extension) at the command prompt: >> prog1

The command window prompt displays the result-

6170

Instance

Create a script file and enter the following code-

a = 5; b = 7;
c = a + b
d = c + sin(b)
e = 5 * d
f = exp(-d)

After compiling and executing the above code, the following result will be produced-

c =  12
d =  12.657
e =  63.285
f =    3.1852e-06