MATLAB COM Builder    

Adding Events to COM Builder Objects

MATLAB COM Builder supports events, or callbacks, through a simple MATLAB language pragma. You simply provide a MATLAB function stub that serves as the prototype for the event, and then provide an implementation of the function in your client code (Visual Basic, C++, etc.). The net effect is that when any other MATLAB function calls the event function, the call is dispatched to the "event handler" in the client code.

You can turn a MATLAB function into an event function by placing a %#event pragma into the code. MATLAB interprets this statement as a comment. When you include the same function as a method on a COM Builder object, the compiler generates an "outgoing interface" for the method, which identifies the method as an event. This outgoing interface is then implemented by the client code. Some examples of how you might use callbacks in your code are

The next example illustrates using a callback in conjunction with a Visual Basic ProgressBar control. The MATLAB function iterate runs through n iterations and fires an event every inc iterations. When the function ends, it returns a single output. To simulate actually doing something, place a pause statement in the main loop so that the function waits for 1 second in each iteration.

Consider the MATLAB functions iterate.m and progress.m.

iterate.m

progess.m

The iterate function runs through n iterations and calls the progress function every inc iterations, passing the current iteration number as an argument. When this function is executed in MATLAB, the value of i is displayed each time the progress function gets called. Suppose you create a COM Builder component that has these two functions included as class methods. This example assumes a component with a single class named myclass. The resulting COM class has a method iterate and an event progress. To receive the event calls implement a "listener" in your Visual Basic code. The VB syntax for the event handler needed for this example is

where aClass is the variable name used for your class instance. The ByVal qualifier is used on all input parameters of an event function. To enable the listening process, dimension the aClass variable with the WithEvents keyword. Refer to the Visual Basic documentation for a complete discussion of VB event processing.

This example is based on a simple VB form with three TextBox controls, one CommandButton control, and one ProgressBar control. The first text box, Text1, inputs the number of iterations, stored in the form variable N. The second text box, Text2, inputs the callback increment, stored in the variable Inc. The third text box, Text3, displays the output of the function when it finishes executing. The command button, Command1, executes the iterate method on your class when pressed. The progress bar control, ProgressBar1, updates itself in response to the progress event.


  Adding Class Properties to COM Builder Objects Creating an Instance of a Class