Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Plot-type selection components

April 28, 2011 5 Comments

Last week, Scott Hirsh, Matlab’s lead product manager, posted an article about Matlab’s redesigned plot catalog. In that post, Scott mentioned the internal deliberations about whether the plot catalog was needed at all. He explained how the final decision was to keep and enhance the catalog, rather than remove it, based on user feedback that the catalog is in fact useful, its main benefit being the fact that it is large and easy to use.
I posted a short comment mentioning my experience of using the catalog programmatically as another use-case that might be taken into consideration. Today, I would like to expand that short comment and explain how built-in plot-selection components can be used in our Matlab GUI programs.

Built-in plot selection components

PlotTypeCombo is a plot-function selector is that is one of my personal favorite controls for embedding in a Matlab application:

% Present the PlotCombo control
import com.mathworks.mlwidgets.graphics.*
jPlotCombo = PlotTypeCombo;
pos = [100,100,170,50];
[jhPlotCombo,hPanel] = javacomponent(jPlotCombo,pos,gcf);
set(jhPlotCombo,'ActionPerformedCallback',@myCBFcn);
% Callback function to process PlotCombo selections
function myCBFcn(jObject,jEventData)
   newPlotFunc = jObject.getSelectedItem.getName.char;
   %Now do something useful with the selected function
end  % myCBFcn

% Present the PlotCombo control import com.mathworks.mlwidgets.graphics.* jPlotCombo = PlotTypeCombo; pos = [100,100,170,50]; [jhPlotCombo,hPanel] = javacomponent(jPlotCombo,pos,gcf); set(jhPlotCombo,'ActionPerformedCallback',@myCBFcn); % Callback function to process PlotCombo selections function myCBFcn(jObject,jEventData) newPlotFunc = jObject.getSelectedItem.getName.char; %Now do something useful with the selected function end % myCBFcn

PlotCatalog is a dialog window that presents the plot functions catalog:

com.mathworks.mlwidgets.graphics.PlotCatalog.getInstance.show

com.mathworks.mlwidgets.graphics.PlotCatalog.getInstance.show

PlotTypeCombo    PlotCatalog
PlotTypeCombo and PlotCatalog

Usage example

For one application that I developed for a client, I implemented a dynamic report page that enables users to select the plotting function using a PlotTypeCombo. When users select a non-default function (e.g. stairs or loglog), that function is automatically added to the combo-box for possible future reuse. I implemented this as follows:

% Add the specified plotFunc to a PlotTypeCombo control
function updatePlotCombo(plotCombo, plotFunc)
   % Convert plotFunc (a Matlab string) into a Java PlotSignature
   import com.mathworks.mlwidgets.graphics.*
   plotSig = PlotMetadata.getPlotSignature(plotFunc);
   % Get the list of all existing plot types in the combo box
   existingPlotTypes = {};
   for plotIdx = 0 : plotCombo.ItemCount-1
      nextItem = plotCombo.getItemAt(plotIdx);
      if isjava(nextItem)
         nextItem = char(nextItem.getName);
      end
      existingPlotTypes = [existingPlotTypes, nextItem];
   end
   % If the new plotType is NOT already in the list
   if isempty(strmatch(plotType,existingPlotTypes,'exact'))
      % Add the new plotType to the list just prior to the end,
      % so that "More plots..." will always be last
      plotCombo.insertItemAt(plotSig,plotCombo.ItemCount-1);
   end
   % Set the currently-selected item to be the requested plotType
   % Note: temporarily disable callbacks to prevent involuntary action
   plotCombo.ActionPerformedCallback = [];
   plotCombo.setSelectedItem(plotSig);
   plotCombo.ActionPerformedCallback = @myCBFcn;  % selection callback
end  % updatePlotCombo

% Add the specified plotFunc to a PlotTypeCombo control function updatePlotCombo(plotCombo, plotFunc) % Convert plotFunc (a Matlab string) into a Java PlotSignature import com.mathworks.mlwidgets.graphics.* plotSig = PlotMetadata.getPlotSignature(plotFunc); % Get the list of all existing plot types in the combo box existingPlotTypes = {}; for plotIdx = 0 : plotCombo.ItemCount-1 nextItem = plotCombo.getItemAt(plotIdx); if isjava(nextItem) nextItem = char(nextItem.getName); end existingPlotTypes = [existingPlotTypes, nextItem]; end % If the new plotType is NOT already in the list if isempty(strmatch(plotType,existingPlotTypes,'exact')) % Add the new plotType to the list just prior to the end, % so that "More plots..." will always be last plotCombo.insertItemAt(plotSig,plotCombo.ItemCount-1); end % Set the currently-selected item to be the requested plotType % Note: temporarily disable callbacks to prevent involuntary action plotCombo.ActionPerformedCallback = []; plotCombo.setSelectedItem(plotSig); plotCombo.ActionPerformedCallback = @myCBFcn; % selection callback end % updatePlotCombo

Usage of this function would then be as simple as invoking the updatePlotCombo() function:

updatePlotCombo(jhPlotCombo,'stairs');

updatePlotCombo(jhPlotCombo,'stairs');

Dynamic plot selection example
Dynamic plot selection example

Plot signatures

The astute reader will have noticed from the updatePlotCombo() function above, that the PlotTypeCombo items are plot-signature objects. Different plot functions expect a different set of input arguments (a.k.a. signature). This information is kept and can be queried from the PlotMetadata class:

>> com.mathworks.mlwidgets.graphics.PlotMetadata.listAllSignatures
ans =
[area, bar, bar (stacked), barh (stacked), barh, comet, compass, errorbar,
 feather, loglog, plot, plot N series, plot N series against T, plot3, plotmatrix, plotyy,
 polar, quiver, quiver3, ribbon, scatter, scatter3, semilogx, semilogy,
 stairs, stem, stem3, null, contour, contour3, contourf, image,
 imagesc, mesh, meshc, meshz, pcolor, plot against first column, surf, surfc,
 surfl, waterfall, null, contour, contour3, contourf, image, imagesc,
 mesh, meshc, meshz, pcolor, plot against first column, surf, surfc, surfl, waterfall]
>> plotFunc = 'surf';  % for example
>> plotSig = PlotMetadata.getPlotSignature(plotFunc);
>> args = cell(plotSig.getArgs)';
args =
    [1x1 com.mathworks.mlwidgets.graphics.PlotArgDescriptor]
    [1x1 com.mathworks.mlwidgets.graphics.PlotArgDescriptor]
    [1x1 com.mathworks.mlwidgets.graphics.PlotArgDescriptor]
    [1x1 com.mathworks.mlwidgets.graphics.PlotArgDescriptor]
>> argsStruct = [];  % initialize
>> for argsIdx = 1 : length(args)
      argsStruct(argsIdx).axis = char(args{argsIdx}.getAxis);
      argsStruct(argsIdx).name = char(args{argsIdx}.getName);
      argsStruct(argsIdx).label = char(args{argsIdx}.getLabel);
      argsStruct(argsIdx).dims = args{argsIdx}.getNumDimensions;
      argsStruct(argsIdx).reqObj = args{argsIdx}.getRequired;
      req = argsStruct(argsIdx).reqObj;
      argsStruct(argsIdx).requiredFlag = req.equals(req.REQUIRED);
   end
>> disp(argsStruct(1))			% Info about the first input argument
            axis: 'X'
            name: 'X'
           label: 'X Data Source'
            dims: [2x1 int32]
          reqObj: [1x1 com.mathworks.mlwidgets.graphics.PlotArgDescriptor$RequiredType]
    requiredFlag: 0
>> disp({argsStruct.axis})		% axis info of all input arguments
    'X'    'Y'    'Z'    ''
>> disp({argsStruct.name})		% name of all input arguments
    'X'    'Y'    'Z'    'C'
>> disp({argsStruct.label})	% data label of all input arguments
    'X Data Source'    'Y Data Source'    'Z Data Source'    'Color'
>> disp({argsStruct.dims})		% dimensionality of all input args
    [2x1 int32]    [2x1 int32]    [2]    [2]
>> disp({argsStruct.requiredFlag})	% is any input arg mandatory?
    [0]    [0]    [1]    [0]

>> com.mathworks.mlwidgets.graphics.PlotMetadata.listAllSignatures ans = [area, bar, bar (stacked), barh (stacked), barh, comet, compass, errorbar, feather, loglog, plot, plot N series, plot N series against T, plot3, plotmatrix, plotyy, polar, quiver, quiver3, ribbon, scatter, scatter3, semilogx, semilogy, stairs, stem, stem3, null, contour, contour3, contourf, image, imagesc, mesh, meshc, meshz, pcolor, plot against first column, surf, surfc, surfl, waterfall, null, contour, contour3, contourf, image, imagesc, mesh, meshc, meshz, pcolor, plot against first column, surf, surfc, surfl, waterfall] >> plotFunc = 'surf'; % for example >> plotSig = PlotMetadata.getPlotSignature(plotFunc); >> args = cell(plotSig.getArgs)'; args = [1x1 com.mathworks.mlwidgets.graphics.PlotArgDescriptor] [1x1 com.mathworks.mlwidgets.graphics.PlotArgDescriptor] [1x1 com.mathworks.mlwidgets.graphics.PlotArgDescriptor] [1x1 com.mathworks.mlwidgets.graphics.PlotArgDescriptor] >> argsStruct = []; % initialize >> for argsIdx = 1 : length(args) argsStruct(argsIdx).axis = char(args{argsIdx}.getAxis); argsStruct(argsIdx).name = char(args{argsIdx}.getName); argsStruct(argsIdx).label = char(args{argsIdx}.getLabel); argsStruct(argsIdx).dims = args{argsIdx}.getNumDimensions; argsStruct(argsIdx).reqObj = args{argsIdx}.getRequired; req = argsStruct(argsIdx).reqObj; argsStruct(argsIdx).requiredFlag = req.equals(req.REQUIRED); end >> disp(argsStruct(1)) % Info about the first input argument axis: 'X' name: 'X' label: 'X Data Source' dims: [2x1 int32] reqObj: [1x1 com.mathworks.mlwidgets.graphics.PlotArgDescriptor$RequiredType] requiredFlag: 0 >> disp({argsStruct.axis}) % axis info of all input arguments 'X' 'Y' 'Z' '' >> disp({argsStruct.name}) % name of all input arguments 'X' 'Y' 'Z' 'C' >> disp({argsStruct.label}) % data label of all input arguments 'X Data Source' 'Y Data Source' 'Z Data Source' 'Color' >> disp({argsStruct.dims}) % dimensionality of all input args [2x1 int32] [2x1 int32] [2] [2] >> disp({argsStruct.requiredFlag}) % is any input arg mandatory? [0] [0] [1] [0]

This signature data can be queried for each separate plot type. In fact, I use this mechanism above so that whenever the user changes the plot-type in the PlotTypeCombo, the usage label text beneath the combo-box is updated with the specific plot’s signature information:

Dynamic plot signature data
Dynamic plot signature data

Related posts:

  1. Date selection components – The JIDE package, pre-bundled in Matlab, contains several GUI controls for selecting dates - this article explains how they can be used...
  2. Font selection components – Several built-in components enable programmatic font selection in Matlab GUI - this article explains how. ...
  3. Color selection components – Matlab has several internal color-selection components that can easily be integrated in Matlab GUI...
  4. Borderless button used for plot properties – A borderless button can be used to add unobtrusive functionality to plot axes...
  5. Listbox selection hacks – Matlab listbox selection can be customized in a variety of undocumented ways. ...
  6. Matlab toolstrip – part 7 (selection controls) – Matlab toolstrips can contain a wide variety of selection controls: popups, combo-boxes, and galleries. ...
GUI Internal component Java Undocumented feature
Print Print
« Previous
Next »
5 Responses
  1. Amjad Elshenawy June 26, 2011 at 04:33 Reply

    I am following the posts utilizing the package “com.mathworks.mlwidgets.graphics” with great interest.
    The posts “Plot-type selection components” & “Color selection components” moved my thoughts towards the question: how can I list all classes of the package “com.mathworks.mlwidgets.graphics”?

    Thanks Yair

    • Yair Altman June 26, 2011 at 06:48 Reply

      @Amjad – simply open the file %matlabroot%\java\jar\mlwidgets.jar with your favorite zip application (WinZip or WinRAR, for example). A JAR file is just a simple ZIP file that contains the separate Java class files.

  2. Specialized Matlab plots | Undocumented Matlab April 11, 2012 at 10:27 Reply

    […] we will find the gallery integrated in the main Matlab Desktop, possibly next to the current plot selector component. […]

  3. Brad Stiritz May 15, 2013 at 16:13 Reply

    Yair,

    Are there any good options for restricting the set of plot types presented in the dialog? In many GUI contexts where the underlying data structure / types are fixed & well-known, I can imagine it wouldn’t make sense to present the entire catalog. Also, do you know if it’s possible to add custom plot types to the presented catalog?

    Thanks,
    Brad

    • Yair Altman May 28, 2013 at 17:31 Reply

      @Brad – as I told you at the conference last week, you can indeed customize which plot types you wish to appear in the PlotTypeCombo – take a look at page 285 of my book.

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitable (6) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top