Newsletters - MATLAB News & Notes
Compiling GUIs and Graphics
New C/C++ Compiler Suite makes application deployment simple
by Peter Webb
MATLAB® has always been an environment dedicated to refining new ideas into real solutions. The introduction of the MATLAB Compiler and Math Library, made sharing these solutions with others much easier, as the Compiler automates the process of turning an M-file into a standalone program and the Math Library enables you to distribute Compiler-generated programs to anyone. Within the past year, The MathWorks released the MATLAB C/C++ Graphics Library, which lets you include MATLAB GUIs and graphics in the programs you redistribute.
In combination, the Graphics Library, Math Library, and MATLAB Compiler provide a complete solution for developers seeking to distribute MATLAB-based applications to colleagues or customers. Together, these products form the C/C++ Compiler Suite, which are to application deployment what MATLAB is to application development.
Features of the C/C++ Compiler Suite
| See a detailed list of C/C++ Compiler Suite features. |
In addition to the large number of functions included in the Compiler Suite, many functions from the MATLAB Toolboxes can be used when developing standalone programs. Though toolbox functions implemented as MEX-files cannot be used with the Compiler Suite, many M-file toolbox functions will compile and run without modification. p> And because redistribution of the libraries is free, once you've developed a MATLAB program, the Suite allows you to easily and inexpensively share the work you've done with others.
Write it in MATLAB, run it anywhere
With the C/C++ Compiler Suite, you can write your program once and then run it anywhere. There's no need to manually port your code to multiple platforms. The Compiler Suite is available on many different types of machines (PCs and all the flavors of UNIX that MATLAB supports). This means you can develop your application on one machine and then deploy it on any of the others (you'll need the Math and Graphics Libraries for each machine you plan on deploying to).
Rapid development, reliable deployment
The best way to appreciate the features of the C/C++ Compiler Suite is to see it in action. The following examples illustrate the scope and variety of applications that you can develop using the C/C++ Compiler Suite. The examples demonstrate the use of two- and three-dimensional graphics, GUI functions (menus and pushbuttons), and the visualization routines coneplot and slice.
Each of these examples took less than a day to design, develop, and debug. MATLAB's application development tools accelerate the process of turning concepts into code; what would be an arduous weeks-long process in C or Fortran can be completed in a matter of hours with MATLAB.
The standalone applications generated by the Compiler Suite work exactly the same way as the original M-files do in MATLAB: the GUIs have the same look and feel, the graphics are indistinguishable from MATLAB figures, and the mathematics are identical to the results in MATLAB. Each of the examples is illustrated with a screen shot of the resulting standalone program, run from the DOS prompt on a PC running Microsoft Windows.
Always the same answer
MATLAB contains a powerful set of two-dimensional plotting routines that the Compiler Suite allows you to incorporate into your standalone programs. Each of the following two demonstration programs illustrates a different aspect of MATLAB's capabilities. The first uses numerical methods to solve an engineering problem, and the second implements an iterative solution to a complex optimization problem. Since the Compiler Suite uses the same underlying code as MATLAB, the standalone versions of these programs produce exactly the same answers as the MATLAB M-files do.
The bending truss demo shows 12 natural vibration modes of a two-dimensional truss, computed via eigenvalue analysis. The demo features a simple GUI and a line drawing of a truss bridge. Choose the vibration mode, click Start, and you'll see the bridge start to shake and quiver. Figure 1 shows the bridge distorted by its 9th vibration mode.
![]() |
|
Fig. 1: Truss bridge distorted by its 9th vibration mode
|
The traveling salesman program searches for an optimal route between the N cities a traveling salesman must visit, arranging his travel so as to minimize the total length of the trip. In computer science circles, this is a very famous problem, with no closed-form solution. The graphics in the demo allow you to watch the solution converge on a local minimum; the solution is not guaranteed to be globally optimal. Figure 2 shows the solution for 50 random locations in the United States.
![]() |
|
Fig. 2: Solution to 50-city traveling salesman problem.
|
The C/C++ Compiler Suite turns each of these demos into a standalone program with a single command. For example, to build a standalone program from the traveling salesman demo, you type the following at the MATLAB prompt:
mcc -B sgl travel
The mcc command invokes the MATLAB Compiler, the -B sgl switch indicates this application requires graphics, and travel is the name of the M-file being compiled. The same command (changing the name of the M-file, of course) works for each of the example programs. To run the resulting executable, simply type its name at the system prompt:
C:\> travel
This command assumes you've running on a Windows machine, but the procedure is much the same on a UNIX machine.
Realistic images and interactive control
MATLAB 5 contains approximately 30 functions for creating and lighting three-dimensional surfaces. In addition to the shape of the surfaces, you have control over the material properties of the surface, the surface color, the light color and direction, and the apparent position of the viewer. Figure 3 shows a realistically rendered model of a dump truck (with a somewhat whimsical cargo-look closely and you'll recognize the Klein bottle, peaks, the knot, and the L-shaped membrane). The truck body consists of over 10,000 polygons, enough to model the truck with a high degree of realism.
![]() |
|
Fig. 3: Truck loaded with MATLAB surfaces.
|
The data for all those polygons is stored in one MAT-file, and the cargo data in a second. A solid modeling program created the truck data, and MATLAB generated the cargo data. Because the C/C++ Compiler Suite supports both MAT-file save and load and the more general MATLAB file I/O routines (fscanf and read, for example), standalone applications can easily read data generated by MATLAB or other programs. Once the camera position and other details of the viewpoint are set up, drawing the truck takes only three lines of code. The first loads the truck body data, the second draws the truck, and the third activates the realistic lighting and shading effects.
load dumpfv
cla,p=patch('faces',f,'vertices',v,'facecolor',...
[.8 .8 .8],'edgecolor','none',...
'clipping', 'off');
lighting gouraud
A similar load command reads in the data for the cargo. Then each surface of the payload requires only a single command to create it. For example, this command creates the knot:
surface('xdata', atx*.6-6, 'ydata',...
aty*.6, 'zdata', atz*.6+4, 'cdata',...
atz*.6+4,'facelighting','gouraud',...
'edgecolor', 'none', 'clipping', 'off');
To write this by hand in C or C++ would require many hundreds of lines of code. You'll be much more productive writing only four lines of M-code and then letting the Compiler Suite write your C code for you.
This is a large and complex figure consisting of many thousands of graphics objects, but the software rendering in MATLAB and the Compiler Suite can redraw this image in about two seconds. This is fast enough for interactive panning, rotation, and zooming to be both practical and very useful. The toolbar (visible at the top of the Fig. 3), has buttons that activate (from left to right) the two zoom modes (zoom in and zoom out) and the 3-D rotation mode. The camera menu gives you the same kind of control over MATLAB's camera as movie directors have over theirs. You can move side to side or up and down without rotating (dolly), rotate around one of the principal axes (roll or orbit), zoom in and out and rotate the camera (pan). The camera menu also gives you immediate control over the appearance of the scene-the kind of shading used on the object surfaces, the way objects are clipped, the visibility of the axis, and the type of illumination used to render the scene. This type of control allows you to quickly and effectively navigate through even very complex scenes. By providing this functionality in standalone mode, the Compiler Suite allows you to create interactive models of your ideas for others to examine, even if they don't have MATLAB.
Scientific visualization
MATLAB has some very powerful three-dimensional plotting functions that simplify the development of sophisticated scientific visualizations. This next example uses coneplot and slice to visualize the speed of a wind flow through a three-dimensional volume. Coneplot generates a three-dimensional field of cones (like a quiver plot) whose length and direction correspond to the wind velocity vector at the point where they're drawn. The slice function displays velocity only, but at a finer grain than the coneplot. The GUI in the example controls the data colormap, the colors of the cones, the height of the horizontal slice, and the number and spacing of the vertical slices. In addition, it determines which visual elements are present-the cones or slices can be turned on or off individually to allow for more thorough exploration of the data. This application requires more coding than the others, but still not that much. The whole M-file, including graphics, GUIs, and callbacks, is only 200 lines long. The heart of the cone plotting is only three lines:
[cx cy cz] = meshgrid(linspace(71,134,9),...
linspace 18,59,9),3:4:15);
h3=coneplot(x,y,z,u,v,w,cx,cy,cz,2);
set(h3, 'facecolor', facecolors(get(colors,...
'value')), 'edgecolor', 'none');
Figure 4 shows the visualization with the horizontal slice at height 7 and three vertical slices. The colormap indicates wind velocity, with magenta being fastest and cyan the slowest.
![]() |
|
Fig. 4: Visualization of wind flow through 3-D volume.
|
Sophisticated plotting routines and an intuitive GUI combine to produce an interactive exploration that much more quickly reveals the character of your data than any static image ever could. MATLAB allows you to build applications like this very quickly, and the Compiler Suite enables you to share them with others effortlessly.
Summary
These example programs range from the simple to the moderately complex, but each one benefited from being a MATLAB application. For example, creating the visualization in MATLAB took less than a single day-it would be the work of weeks in ordinary C or C++. With the C/C++ Compiler Suite, turning it into a standalone program was a matter of a few minutes, no more than the time required to generate and compile the code. The C/C++ Compiler Suite enables you to do your application development in MATLAB and take advantage of both the large number of available functions and the rapid prototyping environment. Then, a single, simple command transforms your MATLAB application into a standalone program that you can deploy or distribute as you see fit. MATLAB has always been a great place to make algorithms-with the C/C++ Compiler Suite, it is now a great place for application development as well.
But don't take our word for it. Contact us or visit www.mathworks.com/web_downloads/ for your free 30-day trial evaluation of the Compiler Suite, and see for yourself how easy it is to turn a MATLAB program into a standalone executable.



