MATLAB Examples

MATLAB is a directory of MATLAB programs which illustrate the use of the MATLAB interactive programming environment.


The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.


Directories related to MATLAB are available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version and a Python version.

Related Data and Programs:

F90_MATLAB, MATLAB functions which are called by a FORTRAN90 program.

MATLAB_C, MATLAB programs which call a C function using the MEX facility.

MATLAB_COMMANDLINE, MATLAB programs which illustrate how MATLAB can be run from the UNIX command line, that is, not with the usual MATLAB command window.

MATLAB_F77, MATLAB programs which call a FORTRAN77 function using the MEX facility.

MATLAB_MOVIES, MATLAB programs which generate animations.

MATLAB_OS, MATLAB programs which invoke an operating system command.

MATLAB_RANDOM, MATLAB programs which illustrate the use of Matlab's random number generators.

Source Code:

CPU_TIMING shows how you can measure the CPU time required for a given computation.

FACTORIAL uses the standard example of the factorial function to demonstrate MATLAB's ability to implement a recursive function.

FILES shows how MATLAB can check whether a directory exists, and create it or remove it.

GLOBAL_VARIABLES shows how two functions can communicate the value of a variable, without using the argument list, by declaring the variable with a GLOBAL statement.

HELLO is a simple example to print out "Hello, world!".

HELLO_ITHACA_QUEUE shows how the "Hello" program can be run on the Ithaca cluster.

INTEGER_PRINT looks at how to print an integer array so that all the digits show up, without a scaling factor.

PERSIST shows how a persistent variable can be used to "remember" the value of some variable within a function between calls.

RAND_TEST looks at how rand() generates uniform random values.

RANDN_TEST looks at how randn() generates normal random values.

RECURSIVE_TEST shows how you can use recursion in a function definition.

SPACING_MATTERS demonstrates that MATLAB can fail to interpret an arithmetic statement correctly because of the occurrence of a blank space. This occurs when creating a vector by listing entries. MATLAB allows the lazy practice of not separating entries by commas, and the result can be an error that is very hard to interpret.

STARTUP is a MATLAB script that can be used to initialize MATLAB. You have to know where to put it, though. (Hint: start MATLAB, and type "pwd" to find out where MATLAB starts by default. That's where you want to put this file.)

You can go up one level to the MATLAB source codes.

Last revised on 08 June 2018.