Click here to Skip to main content
11,428,004 members (59,300 online)
Click here to Skip to main content

Using ActiveX Control Inside MATLAB

, 13 Jan 2004
Rate this:
Please Sign up or sign in to vote.
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.

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

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:

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

1 Object Name MATLAB COM Class
2 Event ID double
3 Start of Event Argument List As passed by control
end - 2 End of Event Argument List (Arg. N) As passed by control
end - 1 Event Structure structure
end Event Name char 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 Name Description Format
Type Event Name char array
Source Control Name MATLAB COM Class
Event ID Event Identifier double
Event Arg Name 1 Event Arg Value 1 As passed by the control
Event Arg Name 2 Event Arg Value 2 As 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

Share

About the Author

A. Riazi
CEO Misbah3Com
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
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 Acqusition 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 competetion, my articles are:

You can see list of my articles, by clicking here


Comments and Discussions

 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 20:53
membermanoj kumar choubey26-Feb-12 20:53 
GeneralActivex control Pin
Member 79781273-Jun-11 7:13
memberMember 79781273-Jun-11 7:13 
GeneralMicrosoft Toolbar ActiveX control Pin
Engineer Anthony30-Apr-07 7:51
memberEngineer Anthony30-Apr-07 7:51 
QuestionVideoSoft FlexArray activeX Pin
Ibrahim Sever8-Nov-06 5:31
memberIbrahim Sever8-Nov-06 5:31 
GeneralactiveX Pin
Anonymous15-May-05 11:39
sussAnonymous15-May-05 11:39 
GeneralReturning a value from an event callback Pin
Balesss7-Mar-05 23:18
memberBalesss7-Mar-05 23:18 
GeneralMATLAB COM Builder Error Pin
walmor8-Oct-04 10:16
susswalmor8-Oct-04 10:16 
GeneralCopied( Matlab Help)+Pasted(into CodeProject):zzz: Pin
Riaazi27-Aug-04 5:15
sussRiaazi27-Aug-04 5:15 
GeneralGuidance Pin
Chivalrous11-Apr-04 20:49
memberChivalrous11-Apr-04 20:49 
GeneralRe: Guidance Pin
A. Riazi12-Apr-04 3:26
memberA. Riazi12-Apr-04 3:26 
GeneralRe: Guidance Pin
Chivalrous13-Apr-04 20:05
memberChivalrous13-Apr-04 20:05 
GeneralRe: Guidance Pin
A. Riazi13-Apr-04 20:31
memberA. Riazi13-Apr-04 20:31 
Generalgreat piece again. Pin
edger14-Jan-04 16:19
memberedger14-Jan-04 16:19 
GeneralRe: great piece again. Pin
A. Riazi15-Jan-04 1:24
memberA. Riazi15-Jan-04 1:24 
GeneralRe: great piece again. Pin
Anonymous9-Aug-05 8:22
sussAnonymous9-Aug-05 8:22 
GeneralRe: great piece again. Pin
Anonymous9-Aug-05 22:13
sussAnonymous9-Aug-05 22:13 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150428.2 | Last Updated 14 Jan 2004
Article Copyright 2004 by A. Riazi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid