This note aims at explaining the difference between programming languages and the natural languages, the type of programming errors and the meaning code debugging how to perform simple arithmetic operations on the MATLAB command line.

The first MATLAB program

The traditional first program in Python language has the following form,

disp('Hello World!')
Hello World!

or if trying on the MATLAB command-line,

disp 'Hello World!'
Hello World!

The MATLAB function disp is equivalent of print() function in Python and many other languages.

Methods of running a MATLAB program

Writing MATLAB script on the MATLAB interpreter’s command prompt

Now, as you may have noticed, in the above example, I used the MATLAB command line to code my first simple MATLAB script. This is one of the simplest and quickest methods of MATLAB scripting and is actually very useful for testing small simple MATLAB ideas and code snippets on-the-fly.

Running MATLAB code written in a MATLAB .m file from MATLAB command prompt

As the program size grows, it wiser to put all of your MATLAB scripts into a single .m file, and then let the MATLAB interpreter run (i.e., interpret) your entire file all at once. To save the above simple “Hello World” MATLAB code in a file and run it, open a new m-file in MATLAB editor, then paste the code in the file and save it in MATLAB’s current working directory as hello.m. Then you can either call your program on the command line like the following,

>> hello
Hello World!

or, simply press F5 button on your keyboard to run it.

Running your MATLAB script file from the Bash/cmd command line

You can also run your codes directly from the Windows cmd or Linux Bash command lines by simply calling MATLAB, followed by a list of optional flags, followed by the name and path to your MATLAB script. For example,

matlab -nodisplay -nodesktop -r "run /home/amir/matlab/hello.m"

Running MATLAB code as a standalone application by compiling it

You can also compile your MATLAB script into a binary file (machine language), which is then standalone and executable independently of MATLAB. This is however out of the scope of our course, but you can read more about it here.

MATLAB interpreter as a simple calculator

One of the greatest advantages of MATLAB is that it can be used as a simple calculator and program interpreter on-the-fly, just like Python, Mathematica, R, and other scripting languages. We will soon see examples of this.