Newsletters - MATLAB News & Notes
MATLAB Compiler and
C/C++Math Libraries 1.2
More functions and easier to use
By Peter Webb
The MATLAB Compiler and C/C++ Math Libraries make MATLAB's algorithms available outside of the interpreted MATLAB environment. The new versions of the MATLAB Compiler and C/C++ Math Libraries offer more functions, are easier to use, and work with MATLAB 5.
Features in version 1.2
The new versions of the MATLAB Compiler and C/C++ Math Libraries feature:- 47 new functions (ODE Suite/Date/Time/String fcns)
- Compiler generation of Simulink CMEX S-functions
- MAT-file access via load() and save()
- Compiler support for multifunction M-files
This is primarily a compatibility release, a bridge between MATLAB 4 and MATLAB 5. While the new version of the MATLAB Compiler runs under MATLAB 5, it does not support multidimensional arrays, cell arrays, structures, objects, variable-length argument lists, or the switch statement. This means you can migrate your MATLAB 4 Compiler development work to MATLAB 5, but you cannot use this version of the MATLAB Compiler on M-files that contain MATLAB 5-specific language features.
Working in the real world: edge detection
The rest of this article describes how to use the MATLAB Compiler and C/C++ Math Libraries to make a MATLAB edge detection routine available outside of MATLAB. First we'll write the edge detection code and then we'll run the MATLAB Compiler to turn it into a complete standalone program or a standalone function for integration with a larger C or C++ program.
Edge detection has many applications in science and industry, from enabling biologists to locate the misshapen cells of sickle-cell anemia to helping industrial robots assemble automobiles. Suppose you want to create an M-file that will perform edge detection on Microsoft Windows Bitmap files. With the Image Processing Toolbox, you only need to write the following short function:
function edgedetect(infile, outfile)
% Read MS Windows bitmap
[x, map] = bmpread(infile);
% Convert to grayscale
inten = ind2gray(x, map);
% Perform edge detection
bw = edge(inten);
% Convert to indexed image
[x, map] = gray2ind(bw);
% Write MS Windows bitmap
bmpwrite(x, map, outfile);
edgedetect() takes the names of two files. The first contains an image to be analyzed, the second stores the output image. The Image Processing Toolbox function edge() locates edges in a grayscale image. The code in the example M-file converts the bitmap into a grayscale image for edge() and turns the results of edge() into another bitmap.
Below, we show how you can use the MATLAB Compiler to turn edgedetect() into either a complete program or a standalone function that any C or C++ program can call.
Example 1: Building a complete program
With the MATLAB Compiler, a single command turns edgedetect() into a complete standalone program. At the MATLAB prompt, type
mcc -mphvg edgedetect
The switches given to the mcc command tell the MATLAB Compiler to build a main program in C++, compile all the M-files called by this M-file, and generate debugging information. This mcc command produces an executable named edgedetect, which you can run from the DOS command line. At the DOS prompt, type
c:\edge> edgedetect satstorm.bmp satedge.bmp
edgedetect reads the image in the input file satstorm.bmp (shown in Figure 1a) and stores the results of the edge detection in the output file satedge.bmp (shown in Figure 1b). In the input image, the storm near the equator on the planet Saturn is hard to see. In the output image, the storm is much more visible, as the edge detection algorithm has removed extraneous detail. The graphics produced by this standalone program are identical to those produced by running the edgedetect in M-file MATLAB.
![]() Figure 1a: Storm on the planet Saturn |
![]() Figure 1b: Result of edge-detection on Figure 1a. |
Example 2: Building a standalone function
With a different set of switches, the MATLAB Compiler generates standalone functions rather than complete programs. Generating a standalone function creates a routine that you can call from anywhere in your large C or C++ program. To test the function, we need a main program. The minimal C++ example code shown below simply calls the function with the names of the input and output files, then exits.
// image.cpp
#include "matlab.hpp"
extern edgedetect(mwArray, mwArray);
void main(int argc, char *argv[])
{
edgedetect("satstorm.bmp","satedge.bmp");
}
Turning the edgedetect M-file into a standalone function and compiling edgedetect and the main program into an executable requires only a single command. At the MATLAB prompt type
mcc -phvg edgedetect image.cpp
Notice that this command passes both an M-file and a C++ file to the MATLAB Compiler (mcc). The MATLAB Compiler translates the M-file edgedetect.m to a C++ file edgedetect.cpp, then runs the system C++ Compiler to build edgedetect.cpp and the user-supplied image.cpp into an executable program named image.
Again, the program runs outside of MATLAB, which means it can be distributed to people who do not have MATLAB. Run the program at the DOS prompt by typing
c:\edge> edgedetect
The graphics produced by this standalone program are identical to those produced by the first example, shown in Figures 1a and 1b.
By automatically translating your M-files to portable C or C++ code, the MATLAB Compiler eliminates time-consuming and error-prone manual translation. In addition, the companion MATLAB C or C++ Math Library provides you with more than 400 MATLAB functions to build applications for distribution to your customers. Contact The MathWorks for details on distributing your applications. Most redistributions are royalty free.
MATLAB has always been a good platform for application development. With the MATLAB Compiler and the C/C++ Math Library, MATLAB becomes an excellent vehicle for application deployment as well.
A free data sheet describing the MATLAB Compiler and C/C++ Math Libraries is available.

