Click here to Skip to main content
15,879,184 members
Articles / Programming Languages / C++
Article

Using ActiveX Control Inside MATLAB

Rate me:
Please Sign up or sign in to vote.
4.71/5 (19 votes)
13 Jan 20046 min read 180.7K   32   17
Employing COM features of MATLAB to host an ActiveX control.

Sample Image - MatlabActivex.jpg

Introduction

Following my article series about MATLAB, I decided to write a new article about MATLAB capabilities to host an ActiveX control. As I described in previous articles, MATLAB has many useful features that developers can benefit from. Here is an introduction on steps to use an ActiveX control within MATLAB environment.

I prefer that our ActiveX control is ready to use. You can create your own ActiveX without any modification, and import it to MATLAB. There is no restriction. Just create your control in a way that you want. No matter that you are creating a graphical ActiveX or a non visible one. In both situations, MATLAB can handle your ActiveX.

With the following MATLAB commands, you can host any ActiveX control in desired figures and set/get any property, invoke any method and handle events.

Step 1: Creating Control

In the first step, you must create an ActiveX control in a figure window. actxcontrol takes PROGID of ActiveX, position and figure handle of a window to create control on figure. The syntax is as below:

h = actxcontrol (progid [, position [, fig_handle [, callback | 
{event1 eventhandler1; event2 eventhandler2; ...} [, filename]]]])

progid is a string that is the name of the control to create. The control vendor provides this string. position is position vector containing the x and y location and the width and height of the control, expressed in pixel units as [x y width height]. Defaults to [20 20 60 60]. fig_handle is the handle of the figure window in which the control is to be created. callback is name of an M-function that accepts a variable number of arguments. event is specified by either number or name. eventhandler is name of a M-function that accepts a variable number of arguments. This function will be called whenever the control triggers the event associated with it. filename is the name of a file to which a previously created control has been saved. When you specify filename, MATLAB creates a new control using the position, handle and event/eventhandler arguments, and then initializes the control from the specified file. The progid argument in actxcontrol must match the PROGID of the saved control.

For example, for creating Media Player ActiveX in a figure, you must call actxcontrol as follows:

m=figure;
h=actxcontrol('MediaPlayer.MediaPlayer.1', [0 0 300 300], m);

'MediaPlayer.MediaPlayer.1' is PROGID of Windows Media Player ActiveX. The control is shown in the specified position.

Step 2: Setting Properties

Now, we must set some properties of the control. To set a property or get property value, we can use MATLAB get and set commands. If you use get command alone, MATLAB will provide you all of properties and their values. Syntax of get and set commands are as below:

v = get(h[, 'propertyname'])
set(h, 'propertyname', value[, 'propertyname2', value2, ...])

h is handle of ActiveX control previously returned from actxcontrol. propertyname is a string that is the name of the property value to be retrieved. And value is the value to which the interface property is set.

For example, to set FileName property of Media Player, we must do this:

set(h, 'filename', 'F:\movies\Aryan.mpg');

If you want to change property of some of values without writing any code, it's better to use a graphical user interface command: inspect. By using inspect command, MATLAB will show you Property Inspector and allow you to change properties in an easy manner. The following figure shows you Property Inspector.

Image 2

Also, you can use propedit command to fire the built-in property editor of the control.

Image 3

Following is the syntax of inspect and propedit commands.

inspect(h)
propedit(h)

h is handle of the ActiveX control.

Step 3: Invoking Methods

Now, it's time to invoke methods. invoke command does it for you.

v = invoke(h, ['methodname' [, arg1, arg2, ...]])

methodname is a string that is the name of the method to be invoked. arg1, ..., argn are arguments, if any, required by the method being invoked.

For example, for playing a movie in Media Player, we should do:

invoke(h, 'play')

If you don't remember the method name, just call methods or methodview commands. For example, methods(h) shows all of the methods that an ActiveX control supports. methodsview shows methods and their arguments:

Image 4

Step 4: Handling Events

events command shows all of the events that an ActiveX control supports. eventlisteners list any events, along with their callback or event handler routines, that have been registered with the control. The function returns a cell array of strings, with each row containing the name of a registered event and the handler routine for that event. If the control has no registered events, then eventlisteners returns an empty cell array. Events and their callback or event handler routines must be registered in order for the control to respond to them. You can register events either when you create the control, using actxcontrol, or at any time afterwards, using registerevent.

events(h)
eventlisteners(h)

An event is fired when a control wants to notify its container that something of interest has occurred. For example, many controls trigger an event when the user single-clicks on the control. In MATLAB, you can create and register your own M-file functions so that they respond to events when they occur. These functions serve as event handlers. You can create one handler function to handle all events or a separate handler for each type of event.

Arguments Passed to Event Handlers

When a registered event is triggered, MATLAB passes information from the event to its handler function as shown in this table.

Argument Number

Contents

Format

1Object NameMATLAB COM Class
2Event IDdouble
3Start of Event Argument ListAs passed by control
end - 2End of Event Argument List (Arg. N)As passed by control
end - 1Event Structurestructure
endEvent Namechar array

When writing an event handler function, use the Event Name argument to identify the source of the event. Get the arguments passed by the control from the Event Argument List (arguments 3 through end-2). All event handlers must accept a variable number of arguments:

function event (varargin)
if (varargin{end}) == 'MouseDown')       % Check the event name 
     x_pos = varargin{5};     % Read 5th Event Argument
     y_pos = varargin{6};     % Read 6th Event Argument
end

Note: The values passed vary with the particular event and control being used.

Event Structure

The second to last argument passed by MATLAB is the Event Structure, which has the following fields.

Field NameDescriptionFormat
TypeEvent Namechar array
SourceControl NameMATLAB COM Class
Event IDEvent Identifierdouble
Event Arg Name 1Event Arg Value 1As passed by the control
Event Arg Name 2Event Arg Value 2As passed by the control
etc.  

For example, when the MouseDown event of the Media Player control is triggered, MATLAB passes this Event Structure to the registered event handler:

Type: 'MouseDown'
Source: [1x1 COM.mwsamp.mwsampctrl.2]
EventID: -605
Button: 1
Shift: 0
x: 27
y: 24

Here is an example:

m=figure
h=actxcontrol('MediaPlayer.MediaPlayer.1', [0 0 300 300], 
               m, {'PlayStateChange', 'myPlayStateChange'});
set(h, 'filename', 'F:\movies\Aryan.DAT')

Now PlayStateChange event is registered and myPlayStateChange function will be called when the state of media is changed. Create new M-file and save it as myPlayStateChange.m. Then type following commands:

function myPlayStateChange(varargin)
disp('Play State Change By User')

When state of media is changed, MATLAB will show "Play State Change By User"!

Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
CEO Solaris Electronics LLC
United Arab Emirates United Arab Emirates
I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acquisition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competition, my articles are:


You can see list of my articles, by clicking here


Comments and Discussions

 
Questioncan we use axis instead of figure Pin
Siva Batchu12-Apr-16 21:00
Siva Batchu12-Apr-16 21:00 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 19:53
professionalManoj Kumar Choubey26-Feb-12 19:53 
GeneralActivex control Pin
Member 79781273-Jun-11 6:13
Member 79781273-Jun-11 6:13 
GeneralMicrosoft Toolbar ActiveX control Pin
Engineer Anthony30-Apr-07 6:51
Engineer Anthony30-Apr-07 6:51 
QuestionVideoSoft FlexArray activeX Pin
Ibrahim Sever8-Nov-06 4:31
Ibrahim Sever8-Nov-06 4:31 
GeneralactiveX Pin
Anonymous15-May-05 10:39
Anonymous15-May-05 10:39 
How can I program Cdlg.Rep.Dlg ActiveX in Matlab ?
GeneralReturning a value from an event callback Pin
Balesss7-Mar-05 22:18
Balesss7-Mar-05 22:18 
GeneralMATLAB COM Builder Error Pin
walmor8-Oct-04 9:16
walmor8-Oct-04 9:16 
GeneralCopied( Matlab Help)+Pasted(into CodeProject):zzz: Pin
Riaazi27-Aug-04 4:15
sussRiaazi27-Aug-04 4:15 
GeneralGuidance Pin
Chivalrous11-Apr-04 19:49
Chivalrous11-Apr-04 19:49 
GeneralRe: Guidance Pin
Abbas_Riazi12-Apr-04 2:26
professionalAbbas_Riazi12-Apr-04 2:26 
GeneralRe: Guidance Pin
Chivalrous13-Apr-04 19:05
Chivalrous13-Apr-04 19:05 
GeneralRe: Guidance Pin
Abbas_Riazi13-Apr-04 19:31
professionalAbbas_Riazi13-Apr-04 19:31 
Generalgreat piece again. Pin
rockonedge14-Jan-04 15:19
rockonedge14-Jan-04 15:19 
GeneralRe: great piece again. Pin
Abbas_Riazi15-Jan-04 0:24
professionalAbbas_Riazi15-Jan-04 0:24 
GeneralRe: great piece again. Pin
Anonymous9-Aug-05 7:22
Anonymous9-Aug-05 7:22 
GeneralRe: great piece again. Pin
Anonymous9-Aug-05 21:13
Anonymous9-Aug-05 21:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.