Documentation Center

  • Trial Software
  • Product Updates


Add Grid ActiveX Control in a Figure

This example adds a Microsoft® ActiveX® spreadsheet control to a figure, which also contains an axes object for plotting the data displayed by the control. Clicking a column in the spreadsheet causes the data in that column to be plotted. Clicking down and dragging the mouse across multiple columns plots all columns touched.

Techniques Demonstrated

  • Registering a control for use on your system.

  • Writing a handler for one of the control's events and using the event to execute MATLAB® plotting commands.

  • Writing a resize function for the figure that manages the control's size as users resize the figure.

Using the Control

This example assumes that your data samples are organized in columns and that the first cell in each column is a title, which is used by the legend. See Complete Code Listing for an example of how to load data into the control.

Once the data is loaded, click the column to plot the data. The following picture shows a graph of the results of Test2 and Test3 created by selecting column B and dragging and releasing on column C.

Complete Code Listing

You can open the file used to implement this example in MATLAB Editor:

Preparing to Use the Control

The ActiveX control used in this example is typical of those downloadable from the Internet. Once you have downloaded the files you need, register the control on your system using the DOS command regsvr32. In a command prompt, enter a command of the following form:

regsvr32 sgrid.ocx

From the MATLAB command line, type:

system 'regsvr32 sgrid.ocx'

See the section Registering Controls and Servers for more information.

Finding the Control's ProgID

Once you have installed and registered the control, you can obtain its programmatic identifier using the ActiveX Control Selector dialog. To display this dialog box, use the actxcontrolselect command. Locate the control in the list and the selector displays the control and the ProgID.

Creating a Figure to Contain the Control

This example creates a figure that contains an axes object and the grid control. The first step is to determine the size of the figure and then create the figure and axes. This example uses the default figure and axes size (obtained from the respective Position properties) to calculate a new size and location for each object.

dfpos = get(0,'DefaultFigurePosition');
dapos = get(0,'DefaultAxesPosition');
hfig = figure('Position',dfpos([1 2 3 4]).*[1 .8 1 1.25],...
   'Name','Select the columns to plot',...
   'ResizeFcn',{@reSize dfpos(3)});
hax = axes('Position',dapos([1 2 3 4]).*[1 4 1 .65]);

The above code moves the figure down from the top of the screen (multiply second element in position vector by .8) and increases the height of the figure (multiply fourth element in position vector by 1.25). Axes are created and sized in a similar way.

Creating an Instance of the Control

Use the actxcontrol function to create an instance of the control in a figure window. This function creates a container for the control and enables you to specify the size of this container, which usually defines the size of the control. See Managing Figure Resize for a specific example.

Specifying the Size and Location

The control size and location in the figure is calculated by a nested function calcSize. This function is used to calculate both the initial size of the control container and the size resulting from resize of the figure. It gets the figure's current position (i.e., size and location) and scales the four-element vector so that the control container is

  • Positioned at the lower-left corner of the figure.

  • Equal to the figure in width.

  • Has a height that is .35 times the figure's height.

The value returned is of the correct form to be passed to the actxcontrol function and the control's move method.

function conSize = calcSize
   fp = get(hfig,'Position');
   conSize = fp([3 4 3 4]).*[0 0 1 .35];
end % conSize

Creating the Control

Creating the control entails the following steps:

  • Calculating the container size

  • Instantiating the control in the figure

  • Setting the number of rows and columns to match the size of the data array

  • Specifying the width of the columns

conSize = calcSize;
hgrid = actxcontrol('SGRID.SgCtrl.1',conSize,hfig);
hgrid.NRows = size(dat,1);
hgrid.NColumns = size(dat,2);
colwth = 4350; hdwth = hgrid.HdrWidth;

Using Mouse-Click Event to Plot Data

This example uses the control's Click event to implement interactive plotting. When a user clicks the control, the MATLAB software executes a function that plots the data in the column where the mouse click occurred. Users can also select multiple columns by clicking down and dragging the cursor over more than one column.

Registering the Event

You need to register events with MATLAB so that when the event occurs (a mouse click in this case), the MATLAB software responds by executing the event handler function. Register the event with the registerevent function:


Pass the event name (Click) and a function handle for the event handler function inside a cell array.

Defining the Event Handler

The event handler function click_event uses the control's GetSelection method to determine what columns and rows have been selected by the mouse click. This function plots the data in the selected columns as lines, one line per column.

It is possible to click down on a column and drag the mouse to select multiple columns before releasing the mouse. In this case, each column is plotted because the event is not fired until the mouse button is released (which reflects the way the author chose to implement the control). The legend function uses the column number stored in the variable cols to label the individual plotted lines. You must add one to cols because the control counts the columns starting from zero.

Note that you implement event handlers to accept a variable number of arguments (varargin).

function click_event(varargin)
[row1,col1,row2,col2] = hgrid.GetSelection(1,1,1,1,1);  
ncols = (col2-col1)+1;
cols = [col1:col2];
   for n = 1:ncols
      hgrid.Col = cols(n);
      for ii = 1:sz(1)
         hgrid.Row = ii;
         plot_data(ii,n) = hgrid.Number;
end % click_event

Managing Figure Resize

The size and location of a MATLAB axes object is defined in units that are normalized to the figure that contains it. Therefore, when you resize the figure, the axes automatically resize proportionally. When a figure contains objects that are not contained in axes, you are responsible for defining a function that manages the resizing process.

The figure ResizeFcn property references a function that executes whenever the figure is resized and also when the figure is first created. This example creates a resize function that manages resizing grid control by doing the following:

  • Disables control updates while changes are being made to improve performance (use the hDisplay property).

  • Calculates a new size for the control container based on the new figure size (calcSize function).

  • Applies the new size to the control container using its move method.

  • Scales the column widths of the grid proportional to the change in width of the figure (SetColWidth method).

  • Refreshes the display of the control, showing the new size.

function reSize(src,evnt,dfp)
% Return if control does not exist (figure creation)
if ~exist('hgrid','var')
% Resize container
hgrid.bDisplay = 0;
conSize = calcSize;
% Resize columns
scl = conSize(3)/dfp;
ncolwth = scl*colwth;
nhdrwth = hdwth*(scl);
hgrid.HdrWidth = nhdrwth;
end % reSize

Closing the Figure

This example uses the figure delete function (DeleteFcn property) to delete the ActiveX object before closing the figure. The MATLAB software calls the figure delete function before deleting the figure, which enables the function to perform any clean up needed before closing the figure. The figure delete function calls the control's delete method.

function figDelete(src,evnt)
Was this topic helpful?