3 Rules for Writing MATLAB Script / M- File Name

MATLAB Programming Feature image

In an earlier post, I have shared steps by steps procedure for saving and running your first MAT file program in MATLAB.

There are some strict rules you should follow for choosing MATLAB script or MAT-file name.

Rules for Writing MATLAB Script / M-File Name

While saving MAT-file, if you don’t choose MATLAB file name as per their standard naming convention, you will see the following error message.

MATLAB will not be able to run the file <matlab_filename> unless you change its name.

To avoid error, follow the rules specified below.

1. Never use the M-file name in the MATLAB function or command and vice-versa.

In the MATLAB, Don’t use the same name in Command Window or M-file’ s name. So, you should check the files name before naming the M-file.

If you want to check the file name you can use the ‘help’ instruction.

>> help <filename_to_check>

For Example,

Suppose, we want to save M-file name as DipsLab. As of now, we don’t know if this filename is used or written in the in MATLAB command earlier.

So, we want to check the M-file name i.e. “DipsLab” by using the help Instruction.

>> help DipsLab

In the Command Window, if this file name is not used, you will see the message as

DipsLab not found.

Thus, you can use this file “DipsLab” name.

2. Never use the number before the file name.

At the beginning of the MAT-file, don’t use the digit or symbol. Otherwise, the files will not be saved.

For example,

 1stock.m  (It is the wrong format)

stock1.m (It is the correct format)

3. Never use blank space in the file name.

Don’t use the space in the file name. But, you can use the underscore ‘_’ between two sentences or file name.

For example,

Electrical Stuff. m (It is the wrong format) 

Electrical_stuff.m or ElectricalStuff. m (It is the correct format)

These are the 3 most important precautions and rules for writing a MATLAB script.

What’s Next?

I have shared the tutorials base on creating a new model and save the model file in MATLAB Simulink. This link as ‘How to Start and Run MATLAB Simulink model? [with Example]’.



Are you looking for job?

Job openings »


19 thoughts on “3 Rules for Writing MATLAB Script / M- File Name”

    • Thanks, Sandy Rubin for reading the tutorials.

      Also, I started the weekly Newsletter, where I share the electrical tutorials, information and useful electrical related posts and kinds of stuff through the mail.

      You can subscribe to my Newsletter to get all updates in your email inbox.

  1. It’s really a cool and useful piece of information. I am satisfied that you shared this useful information with us. Thanks for sharing.


Leave a Comment