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!