Matlab MEX

Matlab is a great programming language and environment because of its ease of use, great visualization, and rapid prototyping abilities.

Matlab has a feature called MEX, Matlab Executables.   See:  Matlab MEX Introduction

MEX files allow Matlab scripts to call user-supplied functions written in C/C++ and Fortran.

* * *

The first step is to install a supported compiler.


Matlab R2011a Compilers

Matlab R2012a Compilers

* * *

Matlab running on Windows 7 can use:

Microsoft Platform SDK

Microsoft Visual C++ 2010

Note that the following update may be required for Windows 7 64-bit systems:

Microsoft Visual C++ 2010 Service Pack 1 Compiler Update for the Windows SDK 7.1

* * *

Next, go to the Matlab Command Window.


>>mex -setup

The C/C++ source code is compiled with Matlab as:

>>mex filename.cpp

* * *

A good MEX tutorial is given at:  Shawn Lankton Online

A useful example is given at:  University of Cambridge

* * *

Here is a sample Matlab script and the C/C++ function which it calls:



Here are the two commands for running the program set in Matlab:

>>mex rainflow_mex_ac.cpp


The program set performs rainflow cycle counting on a time history per  ASTM E 1049-85 (2005).   Rainflow counting is used for fatigue analysis.

* * *

Here is set of scripts for a fatigue damage spectrum for base excitation via rainflow cycle counting:

Here are the commands for running the program set in Matlab:

>>mex rainflow_fds_mex.cpp

Base acceleration case:


Applied force case:


* * *

See also:  Python Rainflow Page

* * *

– Tom Irvine

8 thoughts on “Matlab MEX

  1. Pingback: Rainflow Fatigue Cycle Counting « Vibrationdata

  2. Pingback: Fatigue Damage Spectrum, Time Domain | Vibrationdata

  3. I ask something to you, I got Real Time Data(sampling frequency 25.6kHz)
    I want to plots of SRS or FDS from 10Hz to 10000Hz.
    But Using FDS.exe and fds_main.m program, Plots show 10 to 6088hz
    How can I get the whole plot for using fds.exe or fds_,main
    Please help me how

    • The SRS and FDS are both time domain calculations which require a higher number of points per cycle (natural period) than frequency domain calculations need.

      So you need to increase the sampling rate.


  4. Thank your kindly answer!! I have an other question.
    I wonder that DP Analyzer show the whole plot from 10 Hz to 10 KHz using same data( the number of data is 4095, sampling frequency 25.6 kHz, Nyquist Frequency of Analyzer is 2.56, Sailor said me, It is possible to calculate SRS plot until 10,000 Hz because Nf is 2.56, 25.6 kHz/2.56 = 1 0kHz )

    I think FDS.EXE program which It is very useful for me limits itself until Nf/2 Hz
    Is it possible to plot until Nf frequency using FDS.EXE or fds_main?
    Does it verify that result?

  5. Pingback: Using Random Vibration Testing to Cover Shock Requirements | Vibrationdata

  6. Pingback: Rainflow Fatigue | vibrationdatapython

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s