Skip to Main Content Skip to Search
Accelerating the pace of engineering and science

 

Newsletters - MATLAB Digest

MATLAB® Add-in for Visual Studio:

Using M-files in Visual C++ Development System

by: Ophir Herbst

Introduction

Many MATLAB applications require the usage of Visual C++ Development System as part of the development process. Common examples include generation of stand-alone C/C++ applications from M-files, creation and debugging of C MEX files for MATLAB, and the integration of legacy M-files in C++ applications.

The MATLAB Add-in for Visual Studio automates the task of bringing MATLAB code into Visual C++. It streamlines Visual C++ projects by setting the library and include paths, defining the constants needed, and the libraries to be included for use with compiled M-files. Furthermore, the MATLAB Add-in for Visual Studio uses the MATLAB MCC and MEX commands, eliminating the manual effort of calling them with the right parameters to compile the M- or C- files.

The MATLAB Add-in is a major new tool for MATLAB users. It makes developing hybrid MATLAB and C/C++ applications much easier, and provides tight integration between MATLAB technology and the Visual C++ environment.

With the MATLAB Add-in for Visual Studio You Can:

  • Easily add M-files to an existing Visual C++ project
  • Quickly create stand-alone C or C++ applications from your M-files
  • Create C MEX files or M MEX files with ease
  • Create shared libraries or MEX files from your M-files
  • Reuse your M-files in your C/C++ applications
  • Debug M-file source code rather than MATLAB Compiler generated C/C++ files
  • Automate the process of editing M-files and compiling then into C/C++, all inside Visual C++
  • Examine matrix data while debugging, using the Visual Matrix Viewer
  • Package completed projects for distribution

Before Continuing Make Sure You Have:

  • Installed MATLAB 6, the MATLAB Compiler, MATLAB Graphics Library, MATLAB Math Library and Visual C++ 5.0 or later
  • Run >mbuild -setup from the dos prompt
  • Run >mex -setup from the dos prompt
  • Run >cd(prefdir);mccsavepath; from the MATLAB prompt
  • Installed the MATLAB Add-in into Visual C++ (as per the installation instructions)

Working with MATLAB Add-in for Visual Studio

Visual MATLAB provides several components that automate tasks involved in generation and debugging of Visual C++ applications. Refer to the MATLAB Add-in installation instructions for help with component installation.

MATLAB Project Wizard
The MATLAB Project Wizard automates the creation of Visual C++ projects. It lets you select the type of application the project will generate and select the M-files for inclusion in that project. Using the MATLAB Project Wizard you should be able to easily generate a complete application that compiles and runs successfully.

click to see larger image
 

MATLAB Add-in Toolbar
You can access the major Visual MATLAB components from the
MATLAB Add-in toolbar. The toolbar is available as part of the
Visual C++ Integrated Development Environment, and has the
following buttons:

  • M++-adds additional M-files into an existing project.
  • Matrix Viewer-opens the matrix viewer, which you use
    to view MATLAB matrix variables while debugging M-files or
    C-Mex projects.
  • Packager-packages complete applications for distribution.
  • Help-opens the Visual MATLAB help file
 

Matrix Viewer
The Matrix Viewer enables you to visually view MATLAB matrix variables while debugging Visual C++ applications generated from M-files, or those containing C MEX files. The Matrix Viewer supports most of MATLAB's native types (double/int matrices, sparce matrices, cell arrays, structures etc.) and provides a convenient view of them. The Matrix Viewer allows you to navigate through the matrices stored cell-arrays and structures:

  • To view a variable in a Visual C++ application, press the Matrix Viewer button.
  • To view a variable inside a MATLAB session, use >mviewer varname.

click to see larger image
 

Packager
The packager provides a user-friendly, single-step interface that packages the generated executable and all required DLLs for distribution into a single compressed zip file. This lets you easily distribute your application to colleagues or clients.

Example 1: Creating a Stand-alone Application for Distribution

Example 2: Generating and Debugging C-Mex Files for MATLAB


click to see larger image

(c)2000 by The MathWorks, Inc. MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, and Target Language Compiler are trademarks of The MathWorks, Inc. Visual C++ and Visual Studio are registered trademarks of Microsoft Corporation in the United States and/or other countries.Other product or brand names are trademarks or registered trademarks of their respective holders.

Contact sales
Subscribe to newsletters