MATLAB SGL Lib
MATLAB SGL Lib
MATLAB C/C++
Graphics Library
The Language of Technical Computing
Computation
Visualization
Programming
User’s Guide
Version 2
How to Contact The MathWorks:
www.mathworks.com Web
comp.soft-sys.matlab Newsgroup
508-647-7000 Phone
508-647-7001 Fax
For contact information about worldwide offices, see the MathWorks Web site.
Preface
Related Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
Introduction
1
Components of the MATLAB C/C++ Graphics Library . . . . 1-3
Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6
i
Building Graphics Applications on a UNIX System . . . . . . . . . 2-7
Running the MATLAB Compiler Outside MATLAB . . . . . . . . . 2-9
Compiling and Linking Without mbuild . . . . . . . . . . . . . . . . . . . 2-9
Troubleshooting
3
Using Unsupported MATLAB 6.0 Features . . . . . . . . . . . . . . . 3-3
ii Contents
Preface
Using This Guide . . . . . . . . . . . . . . . . . . . ii
Expected Background . . . . . . . . . . . . . . . . . . ii
Organization of the Document . . . . . . . . . . . . . . ii
Related Products . . . . . . . . . . . . . . . . . . . iv
Preface
Chapter Description
ii
Typographical Conventions
Typographical Conventions
We use some or all of these conventions in our manuals.
Menu names, menu items, and Boldface with an initial Choose the File menu.
controls capital letter
iii
Preface
Related Products
The MathWorks provides several products that are especially relevant to the
kinds of tasks you can perform with the MATLAB C/C++ Graphics Library.
For more information about any of these products, see either:
Product Description
iv
1
Introduction
Components of the MATLAB C/C++ Graphics Library . 1-3
Files Installed on PCs . . . . . . . . . . . . . . . . . 1-3
Files Installed on UNIX Systems . . . . . . . . . . . . 1-5
Restrictions . . . . . . . . . . . . . . . . . . . . 1-6
Note You may freely distribute applications you develop with the MATLAB
C/C+ Graphics Library, subject to The MathWorks software license
agreement.
After reading these sections, see “Configuring the MATLAB C/C++ Graphics
Library” on page 1-10.
1-2
Components of the MATLAB C/C++ Graphics Library
• MATLAB 6.0 built-in graphics functions, such as surf, plot, get, and set
• Some commonly used MATLAB 6.0 M-file graphics functions, such as
newplot, gcf, gca, gco, and gcbf
1-3
1 Introduction
1-4
Components of the MATLAB C/C++ Graphics Library
1-5
1 Introduction
Restrictions
The MATLAB C/C++ Graphics Library supports most MATLAB 6.0 features,
including multidimensional arrays, cell arrays, and structures. However, there
are some MATLAB features the graphics library does not support, including:
• MATLAB objects
• MATLAB Java objects
• plotedit command
• propedit command
Note The graphics library is subject to the same limitations as the MATLAB
Compiler. For example, MATLAB functions that require the MATLAB
interpreter, most notably eval() and input(), are not fully supported. See the
MATLAB Compiler documentation for information about their restrictions.
1-6
Restrictions
Table 1-3 lists the device drivers supported by the graphics library. For more
information about specifying device drivers, see the “Printing MATLAB
Graphics” section in MATLAB Graphics documentation.
Device Description
1-7
1 Introduction
the graphics library. For a complete list of print command options, see the
“Printing MATLAB Graphics” section in MATLAB graphics documentation.
Table 1-4: print Command Line Options Supported by the Graphics Library
Option Description
1-8
System Requirements
System Requirements
Software. To use the MATLAB C/C++ Graphics Library to create a stand-alone
C or C++ application requires several other MATLAB products:
You must also have installed on your system an ANSI C or C++ compiler.
1-9
1 Introduction
• The ANSI C or C++ compiler you intend to use to compile the code generated
by the MATLAB Compiler
• The libraries you want to link your application with; specifically, the
MATLAB C/C++ Math Library alone, or the math library and the MATLAB
C/C++ Graphics Library together.
1-10
Configuring the MATLAB C/C++ Graphics Library
Select a compiler:
[1] Borland C++Builder version 5.0
[2] Borland C++Builder version 4.0
[3] Borland C++Builder version 3.0
[4] Borland C/C++ version 5.02
[5] Borland C/C++ version 5.0
[6] Borland C/C++ (free command line tools) version 5.5
[7] Lcc C version 2.4
[8] Microsoft Visual C/C++ version 6.0
[9] Microsoft Visual C/C++ version 5.0
[10] Microsoft Visual C/C++ version 4.2
[0] None
Compiler: 9
1-11
1 Introduction
1-12
Configuring the MATLAB C/C++ Graphics Library
mbuild -setup
Options files control which compiler to use, the compiler and link
command options, and the run-time libraries to link against.
To override the default options file, use the 'mbuild -f' command
(see 'mbuild -help' for more information).
1: /matlab/bin/mbuildopts.sh :
Build and link with MATLAB C/C++ Math Library
Enter the number of the options file to use as your default options
file: 1
1-13
1 Introduction
1-14
2
Creating Stand-Alone
Graphics Applications
Overview . . . . . . . . . . . . . . . . . . . . . 2-3
This chapter describes how to use the MATLAB C/C++ Graphics Library to
build stand-alone graphics applications. It provides:
2-2
Overview
Overview
You use the MATLAB Compiler (mcc) to create a stand-alone C or C++ graphics
application. In this process, the MATLAB Compiler:
2-3
2 Creating Stand-Alone Graphics Applications
MATLAB Compiler
C or C++ compiler
2-4
Building a Stand-Alone Graphics Application
C applications <MATLAB>\toolbox\compiler\bundles\sgl
!copy <MATLAB>\toolbox\matlab\demos\lorenz.m .
!lorenz
2-5
2 Creating Stand-Alone Graphics Applications
Results of Compilation
The MATLAB Compiler generates multiple C or C++ source code modules in
your current working directory. These include wrapper files that contain
necessary components of a stand-alone application, such as a main() entry
point.
In addition, the first time you run the MATLAB Compiler to create a
stand-alone graphics application, it creates a subdirectory, named \bin, in
your current working directory. The Compiler puts in this directory versions of
the MATLAB menu bar and toolbar figure files that are used by stand-alone
graphics applications at run-time. (Stand-alone graphics applications use a
special menu bar and toolbar. For more information, see “Changes in Run-Time
Behavior and Appearance” on page 2-11.) Subsequently, whenever you run the
Compiler, it checks for the existence of these files in the \bin directory and does
not overwrite them if they exist.
2-6
Building a Stand-Alone Graphics Application
Editing the Search Path on Windows 95. On Window 95 systems, you must edit your
autoexec.bat file to add the shared library directory to the PATH variable.
Editing the Search Path on Windows NT. On Windows NT systems, go the Settings
option on the Start menu and choose Control Panel. Double-click on the
System icon to view the System Properties dialog box. Use the Environment
panel to edit the PATH variable.
C applications <MATLAB>/toolbox/compiler/bundles/sgl
!cp <MATLAB>/toolbox/matlab/demos/lorenz.m ./
!lorenz
2-7
2 Creating Stand-Alone Graphics Applications
• The example uses the UNIX cp command to copy the Lorenz application
M-file into the current MATLAB directory. Use the ! symbol to execute an
operating system command inside the MATLAB environment. (This is
suggested because you may not have permission to create a new file in the
MATLAB demos directory.) Replace <MATLAB> with the name of your top-level
MATLAB installation directory.
• The example invokes the MATLAB Compiler, using the -B flag to specify the
bundle used to create C stand-alone graphics applications, sgl
Results of Compilation
The MATLAB Compiler generates multiple C or C++ source code modules in
your current working directory. These include wrapper files that contain
necessary components of a stand-alone application, such as a main() entry
point.
In addition, the first time you run the MATLAB Compiler to create a
stand-alone graphics application, it creates a subdirectory, named /bin, in
your current working directory. The Compiler puts in this directory versions of
the MATLAB menu bar and toolbar figure files that are used by stand-alone
graphics applications at run-time. (Stand-alone graphics applications use a
special menu bar and toolbar. For more information, see “Changes in Run-Time
Behavior and Appearance” on page 2-11.) Subsequently, when you run the
Compiler, it checks for the existence of these files in the /bin directory and does
not overwrite them if they exist.
2-8
Building a Stand-Alone Graphics Application
Architecture Command
where:
<MATLAB> is the MATLAB root directory.
<ARCH> is your system architecture
To avoid having to reissue this command at the start of each login session,
include it in a startup script such as ~/.cshrc or ~/.login. Use the ~/.login
option, if your system supports it, because it only gets executed once.
2-9
2 Creating Stand-Alone Graphics Applications
To determine the libraries you need to link with, use the mbuild command with
the -n option. When you specify this option, mbuild sets up the compile and link
command lines necessary to build a stand-alone application but does not
execute the commands. View the output of mbuild -n to determine the list of
libraries you must link your application with and the order in which you must
specify them.
You can also specify this mbuild option on the MATLAB Compiler command
line by specifying -M option.
mcc -M -n -B sgl lorenz.m
Note On PCs, if you are using the Microsoft Visual C compiler, you must
manually build import libraries from the .def files using the lib command. If
you are using the Borland C compiler, you can link directly against the .def
files using the implib command. See your compiler documentation for
information about these commands.
2-10
Changes in Run-Time Behavior and Appearance
2-11
2 Creating Stand-Alone Graphics Applications
2-12
Changes in Run-Time Behavior and Appearance
Note The graphics library includes the Print option in the File menu of
stand-alone graphics applications. However, the Print option in stand-alone
applications does not display the Print dialog box, as it does for M-file
applications.
Ctrl+C Handling
When you run a graphics application within MATLAB, you can press Ctrl+C
to break infinite loops. For example, you can press Ctrl+C to stop an
animation. When you run a C or C++ stand-alone application, Ctrl+C handling
is not supported.
2-13
2 Creating Stand-Alone Graphics Applications
PCs <MATLAB>\extern\lib\win32\mglinstaller.exe
2-14
Distributing Stand-Alone Graphics Applications
• Run the MATLAB Math and Graphics Run-Time Library Installer. This
program extracts the libraries from the archive and installs them in
subdirectories of a directory specified by the user.
• Add the bin/$ARCH subdirectory to their path. This is the only MATLAB
run-time library subdirectory that needs to be added to the path.
Note If a customer already has the MATLAB math and graphics run-time
libraries installed on their system, they do not need to reinstall them. They
only need to ensure that the library search path is configured correctly.
On UNIX Systems
On UNIX systems, your customers run the MATLAB Math and Graphics
Run-Time Library Installer by executing the mglinstaller command at the
system prompt. Your customers can specify the name of the directory into
which they want to install the libraries. By default, the installer puts the files
in the current directory.
After the installer unpacks and uncompresses the libraries, your customers
must add the name of the bin/<ARCH> subdirectory to the LD_LIBRARY_PATH
environment variable. (The equivalent variable on HP-UX systems is the
SHLIB_PATH.)
On PCs
On PCs, your customers can run the MATLAB Math and Graphics Run-Time
Library Installer by double-clicking on the mglinstaller.exe file. Your
customers can specify the name of the directory into which they want to install
the libraries. By default, the installer puts the files in the current directory.
2-15
2 Creating Stand-Alone Graphics Applications
After the installer unpacks and uncompresses the libraries, your customers
must add the bin\win32 subdirectory to the system path variable (PATH).
For example, if your customers specify the installation directory
mgl_runtime_dir, then they must add mgl_runtime_dir\bin\win32 to PATH.
2-16
3
Troubleshooting
Using Unsupported MATLAB 6.0 Features . . . . . . 3-3
The MATLAB Compiler can compile most M-files that use graphics into
stand-alone applications. Some M-files, however, may include coding practices
that are not supported by the Compiler or by the graphics library. In some
cases, the MATLAB Compiler may not be able to translate the M-file into C or
C++ code. In other cases, the M-file may compile successfully, but fail when run
as a stand-alone application.
This chapter describes how to diagnose and correct these problems. It includes
these sections:
3-2
Using Unsupported MATLAB 6.0 Features
See the MATLAB Compiler documentation for more information about these
limitations.
Symptom
The Compiler outputs error messages that identify which unsupported feature
prevented compilation.
Workaround
If your application uses unsupported features, the only workaround is to
remove these unsupported features by recoding your application.
3-3
3 Troubleshooting
Symptom
If you attempt to compile a script, the Compiler outputs the error message
??? Error: File “filename” is a Script M-file and cannot be
compiled with the current Compiler.
Workaround
To compile an application written as a script, turn it into a MATLAB function.
To do this, include the MATLAB function prototype at the top of the file. You
must also find where the script depends on variables in the base workspace and
declare these variables as global variables.
For example, in the following script, the variable f, set by the call to the figure
function, exists in the base workspace. This variable is then passed as a
parameter to the function, my_func, specified in the callback property string.
Passing a workspace variable in a callback string is not supported by the
MATLAB Compiler.
f = figure;
p_btn = uicontrol(gcf,...
'style', 'pushbutton',...
'Position',[10 10 133 25 ],...
'String', 'Press Here',...
'CallBack','my_func(f);');
global f;
f = figure;
3-4
Compiling Applications Written as Scripts
p_btn = uicontrol(gcf,...
'style', 'pushbutton',...
'Position',[10 10 133 25 ],...
'String', 'Press Here',...
'CallBack','my_callback');
Here is the new callback function. Note how the function also declares f as
a global variable.
function my_callback()
% revised callback
global f;
my_func(f);
3-5
3 Troubleshooting
Symptom
Your application runs, but an interactive user interface element, such as a
push button, is unresponsive. When you close the application, the graphics
library issues this error message:
An error occurred in the callback : change_colormap
The error message caught was : Reference to unknown function
change_colormap from FEVAL in stand-alone mode.
Workaround
To eliminate this error, create a list all of the functions that are specified only
in callback strings and pass this list to the %#function pragma. (See “Finding
Missing Functions in an M-File” on page 3-7 for hints about finding functions
in callback strings.) The Compiler processes any function listed in a
%#function pragma.
%#function change_colormap
peaks;
p_btn = uicontrol(gcf,...
'style', 'pushbutton',...
3-6
Fixing Callback Problems: Missing Functions
Note Instead of using the %#function pragma, you can specify the name of
the missing M-file on the Compiler command line. For more information about
this mechanisms, see the MATLAB Compiler documentation.
3-7
3 Troubleshooting
Workaround
Replace the menu bar and toolbar figure files in your application bin directory
with the versions of these figure files in the MATLAB installation directory
(<MATLAB>\extern\include on PCs or <MATLAB>/extern/include on UNIX
systems). When you restart your stand-alone graphics application, it will use
the new figure files.
Another way to replace your existing figure files with new figure files is to
delete your application bin directory and run the Compiler. If this directory
does not exist, the Compiler creates it and populates it with copies of the figure
files stored in the MATLAB installation directory. There is no need to
recompile your graphics M-file application, especially if this is a
time-consuming task. Compiling a trivial M-file graphics application is enough
to cause the creation of a new application bin directory.
3-8
Depending on Graphics Settings in Start-Up Files
Workaround
If your application depends on graphics settings in a startup.m file, include the
startup.m, or the portion of it your application depends on, in the group of
M-files that you compile with the MATLAB Compiler. Your application must
explicitly call these graphics settings.
3-9
3 Troubleshooting
Note Customers of your application may encounter the same problem if they
have this DLL installed on their system.
Symptom
Your application compiles and an executable is created. However, when you
invoke it, no figure window appears. If you run the application from a DOS
command window, you or one of your customers may see an error message such
as:
The UIW_SGL.DLL file is linked to missing export MFC42.DLL:####.
or
The ordinal #### could not be located in the dynamic-link library
MFC42.dll.
Workaround
To fix this problem, locate the files named MFC42.dll and MSVCRT.dll in your
Windows system directory and replace them with the versions of these files in
the <MATLAB>\bin\win32 directory, where <MATLAB> represents the name of
your MATLAB installation directory.
Your customers who encounter the same problem should also replace the
versions of these files in the Windows system directory. They can use the
versions they find in <MGLRUNTIMELIBRARY>\bin\win32 directory, where
<MGLRUNTIMELIBRARY> is the name of the directory in which they installed the
MATLAB Math and Graphics Run-Time Libraries. See ‘‘Distributing
Stand-Alone Graphics Applications’’ on page 2-14 for more information.
You may also encounter this problem with the files dforrt.dll or dformd.dll.
You can replace these files in the Windows system directory with their
counterparts in the <MGLRUNTIMELIBRARY>\bin\win32 directory.
3-10
Index
Symbols
%#function pragma 3-6 compiling graphics applications
.cshrc 2-9 on PCs 2-5
.login 2-9 on UNIX systems 2-7
/bin directory (UNIX) without using mbuild 2-9
creating 2-8 configuring the graphics library
removing 3-8 on PCs 1-10
\bin directory (PCs) on UNIX systems 1-12
creating 2-6 conventions in our documentation (table) iii
removing 3-8 Ctrl+C handling
stand-alone graphics applications 2-13
A
Adobe Illustrator D
device driver 1-7 device drivers
axes objects 3-7 support 1-6
distributing applications
packaging 2-14
B drivers
-B flag support 1-6
specifying bundle files on PCs 2-5 Dynamic Link Libraries (DLLs)
specifying bundle files on UNIX systems 2-7 installed with graphics library 1-3
building stand-alone graphics applications
on PCs 2-5
on UNIX systems 2-7 E
other methods 2-9 encapsulated PostScript
bundle files support 1-7
defined 2-7 eval
in stand-alone graphics applications 3-3
restrictions 1-6
C example application
callback strings flames.m 1-4, 1-5
passing workspace variables in 3-5
searching M-files for 3-7
color printing F
support 1-7 figure objects 3-7
Compiler. See MATLAB Compiler 1-9 FigureMenuBar.fig 1-4, 1-5
I-1
Index
J
G Java objects
Ghostscript drivers in stand-alone graphics applications 3-3
support 1-6
global variables 3-4
graphics applications L
build procedure 2-5 LD_LIBRARY_PATH
overview 2-3 editing 2-8
run-time behavior 2-11 run-time libraries 2-15
trouble starting 3-10 libmwsgl.sl 1-5
unsupported coding practices 1-8 libmwsgl.so 1-5
gui_sgl.dll 1-4 LIBPATH
run-time libraries 2-15
library search path
H specifying on PCs 2-7
Handle Graphics specifying on UNIX systems 2-8
Callback property 3-7 libsgl.h 1-4
defaults 3-9 licensing
objects 3-7 stand-alone graphics applications 2-14
hardcopy_sgl.dll 1-4 linking
hardware requirements 1-9 stand-alone graphics applications 2-9
header files
libsglm.h 1-4
sgl.h 1-5 M
Help MATLAB C/C++ Graphics Library
in stand-alone graphics applications 2-13 components 1-3
hg_sgl.dll 1-4 configuration 1-10
overview 1-2
relationship to the MATLAB Math Libraries
I 2-4
Info button restrictions 1-6
I-2
Index
I-3
Index
I-4