Newsletters - MATLAB News & Notes
MATLAB Compiler and
New Support for ActiveX in MATLAB 5.2
ActiveX technology and MATLAB gives unprecedented power to the M-file programmer
By Chris Norman and Jason Kinchen
Editor's Note: The M-file code in this article has been modified from the printed version released in August 1998.
ActiveX is a suite of Microsoft Windows technologies for combining components from different applications. While MATLAB 5.0 provided support for treating MATLAB as an ActiveX automation server, Version 5.2 supports additional ActiveX technologies, including automation client capabilities and control containment.
The ActiveX client and control containment capability of MATLAB 5.2 puts unprecedented power in the hands of the M-file programmer. Users of tools such as Visual Basic, PowerBuilder, and Delphi have used available ActiveX controls and servers to great effect. Now, MATLAB programmers can put these components to use and combine them with the traditional MATLAB analytical and graphical capabilities.
This article describes two ways to apply ActiveX technology to your MATLAB applications.
Using MATLAB as an automation client
Perhaps you use both Microsoft Excel and MATLAB, but would prefer to work with a single interface-MATLAB. Your data, however, must appear in an Excel spreadsheet. The M-code statements below start Microsoft Excel as an automation server and populate an Excel worksheet with MATLAB data.
![]() |
| An example of an embedded ActiveX application displaying weather data in a single MATLAB figure window. The maps are displayed using Microsoft Explorer controls. Additional knob controls select the city and the type of weather data the user can see. MATLAB graphics (right) show temperature and barometric pressure in a single window. |
To use an ActiveX server, you need to find out its name, or PROGID, and the names and arguments of the properties and methods the server supports. This information can usually be found in the server's documentation. We consulted the Microsoft Office 97 Visual Basic Programmer's Guide and found that Excel's PROGID is Excel.Application.
To launch Excel as an automation server with MATLAB, execute the following command:
hExcel = actxserver ('Excel.Application');
The actxserver command returns a MATLAB ActiveX object. However, when Excel is launched as an automation server, it is invisible by default. To see the main Excel window on the screen, we need to set the Visible property provided in the Excel server.
set (hExcel, 'Visible', 1);
This sets the Visible property on the ActiveX object hExcel to the value 1, which causes Excel to display its main window on the desktop. Alternatively, we can use "dot" notation to set properties, as follows:
hExcel.Visible = 1;
Next, we need to get Excel to create a new workbook. To do this, we invoke the Add method of the Workbooks property of the Excel object.
hExcel.Workbooks.Add;
This creates the new workbook and makes the first worksheet the active sheet. One of the properties of the active sheet is a "cell" object, which we obtain as follows:
hCells = hExcel.ActiveSheet.Cells;
When we have set the Item property of the hCells object with data, it will show up in the Excel worksheet.
a = [100 200 300 400];
for i = 1:length(a)
set (hCells, 'Item', i, 1, a(i));
end
![]() |
|
Microsoft's calendar control embedded in a MATLAB figure window.
|
Embedding ActiveX controls in MATLAB
In the previous example, we used MATLAB to control an entirely separate process-Excel. In the next example, we will not only control but also embed a separate program in MATLAB, as if it were native to MATLAB.
Here, we will embed a calendar control in a MATLAB figure window. The syntax is similar to that used for creating the server, except that we specify the window and location of the control within it. The PROGID of the control is MSCAL.Calendar.
hCalendar = actxcontrol('MSCAL.Calendar',
[50 50 300 200], gcf);
(Chances are that this control already exists on your system, as it is used in many Microsoft applications.) The ActiveX control returns a handle to an ActiveX object, and you can interact with it in much the same way as in the Excel server example.
To obtain the current year, enter
hCalendar.year
To set the current month, enter
hCalendar.month = 4;
You may also invoke methods on the control object by entering
invoke(hCalendar, 'previousyear');
This causes the calendar control to display the date as of a year ago.
What's New in Release 10 (MATLAB 5.2)?The Release 10 CD contains the entire MathWorks product family, a total of 38 products built on MATLAB 5.2 and Simulink 2.2 MATLAB 5.2
Compiler and Libraries (see story in this edition)
Enhanced Toolboxes and Blocksets
Simulink 2.2
Header and footer annotation in printouts of models Also Included in Release 10
Power System Blockset 1.0 for Simulink--New Product! (see story in this edition)
|
Free data sheets describing The MathWorks product family are available

