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 (email)
  •  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
ActiveX (6) 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) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
  • Marcel (9 days 17 hours ago): Hi, I am trying to set the legend to Static, but this command seems not to work in R2022a anymore: set(gca,’LegendColorbarL isteners’,[]); Any ideas? THANKS / marcel
  • Gres (9 days 21 hours ago): In 2018b, you can get the icons by calling [hh,icons,plots,txt] = legend({‘Line 1’});
  • Yair Altman (11 days 16 hours ago): @Mitchell – in most cases the user wants a single string identifier for the computer, that uniquely identifies it with a distinct fingerprint that is different from any...
  • Mitchell (12 days 1 hour ago): Great post! I’m not very familiar with the network interfaces being referenced here, but it seems like the java-based cross-platform method concatenates all network...
  • Yair Altman (14 days 18 hours ago): Dani – You can use jViewport.setViewPosition(java .awt.Point(0,0)) as I showed in earlier comments here
  • dani (15 days 14 hours ago): hi!! how i can set the horizontal scrollbar to the leftside when appearing! now it set to right side of text
  • Yair Altman (24 days 10 hours ago): Dom – call drawnow *just before* you set hLine.MarkerHandle.FaceColorTy pe to 'truecoloralpha'. Also, you made a typo in your code: it’s truecoloralpha, not...
  • Dom (25 days 9 hours ago): Yair I have tried your code with trucoloralpha and the markers do not appear transparent in R2021b, same as for Oliver.
  • Yair Altman (28 days 16 hours ago): Ren – This is usually the expected behavior, which avoids unnecessary duplications of the Excel process in CPU/memory. If you want to kill the process you can always run...
  • Yair Altman (29 days 6 hours ago): When you use plot() without hold(‘on’), each new plot() clears the axes and draws a new line, so your second plot() of p2 caused the first plot() line (p1) to be...
  • Cesim Dumlu (35 days 14 hours ago): Hello. I am trying to do a gradient plot for multiple functions to be displayed on the same axes and each one is colorcoded by respective colordata, using the same scaling. The...
  • Yair Altman (43 days 17 hours ago): @Veronica – you are using the new version of uitree, which uses HTML-based uifigures, and my post was about the Java-based uitree which uses legacy Matlab figures. For...
  • Veronica Taurino (43 days 17 hours ago): >> [txt1,txt2] ans = ‘abrakadabra’
  • Veronica Taurino (43 days 17 hours ago): Hello, I am just trying to change the uitree node name as you suggested: txt1 = 'abra'; txt2 = 'kadabra'; node.setName([txt1,txt2]); >> "Unrecognized method, property, or...
  • Yair Altman (46 days 17 hours ago): The version of JGraph that you downloaded uses a newer version of Java (11) than the one that Matlab supports (8). You need to either (1) find an earlier version of JGraph that...
Contact us
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