MATLAB: Is there a way to programmatically insert the only the zoom, pan, and data cursors in MATLAB

buttonsfigureguiMATLABtoolbarzoom

My question is similar to this one: Disabling SAVE button on GUIDE-generated GUIs
However, I am creating a GUI programmatically, and I would like to just add the buttons from the default figure toolbar that I want (zoom in, zoom out, pan, data cursor) rather than add the whole toolbar then delete the buttons I don't want. It would be also be nice to be able to place the buttons wherever I wanted on the GUI but that is not a must have.
So far, I have ALMOST achieved the minimum goal by turning the figure toolbar on, then deleting ALMOST all the buttons. I just can't figure out how to delete the one that has the tooltip "Brush/Select Data". For example, if I do a finall(fh,'ToolTip','Brush/Select Data'), it will find the "ToggleSplitTool", but it doesn't return a handle to it, so I can't delete it.
% Get a handle to the standard plot toolbar.
tbh = findall(fh,'Type','uitoolbar');
% Get handles to each button we don't want on the standard toolbar and DELETE
ptPlotToolsOn = findall(fh,'Tag','Plottools.PlottoolsOn'); delete(ptPlotToolsOn);
ptPlotToolsOff = findall(fh,'Tag','Plottools.PlottoolsOff'); delete(ptPlotToolsOff);
ttInsertLegend = findall(fh,'Tag','Annotation.InsertLegend'); delete(ttInsertLegend);
ttInsertColorbar = findall(fh,'Tag','Annotation.InsertColorbar'); delete(ttInsertColorbar);
ttLinking = findall(fh,'Tag','DataManager.Linking'); delete(ttLinking);
ttRotate = findall(fh,'Tag','Exploration.Rotate'); delete(ttRotate);
ttEditPlot = findall(fh,'Tag','Standard.EditPlot'); delete(ttEditPlot);
ptPrintFigure = findall(fh,'Tag','Standard.PrintFigure'); delete(ptPrintFigure);
ptSaveFigure = findall(fh,'Tag','Standard.SaveFigure'); delete(ptSaveFigure);
ptFileOpen = findall(fh,'Tag','Standard.FileOpen'); delete(ptFileOpen);
ptNewFigure = findall(fh,'Tag','Standard.NewFigure'); delete(ptNewFigure);
% Get the handle for the zoom in button to remove the separator bar
ttZoomIn = findall(fh,'Tag','Exploration.ZoomIn');
set(ttZoomIn,'Separator','off')
It seems like it may be possible with this: uitoolbar, but the only example given by TMW documentation is pretty useless.
Any ideas how to delete that ToggleSplitTool? Or are there better ways to do this? This seems a little clunky. I'd rather make a custom toolbar and add back the buttons that I want to use with their associated default native callbacks… I'm not sure if I need to get all the Cdata from those buttons to make new ones that do the same thing but are located elsewhere on the figure, etc. I think the callbacks are just part of the putdowntext function, so they are accessible, but I haven't tried going this far yet.

Best Answer

Hello Ryan,
One trick you can use, rather than having to search for each button by tag, is to turn all handle visibility to 'on' in the graphics root . Then you can access the toolbar and all the buttons from the 'Children' property of the figure and toolbar respectively. You can use "findobj" to search within the graphics list for the buttons you want, and then use the "setdiff" function to determine all of the buttons to delete from that.
set(groot,'ShowHiddenHandles','on')
fh = figure;
tbh = findobj(fh.Children,'Type','uitoolbar');
ttZoomIn = findobj(tbh.Children,'Tag','Exploration.ZoomIn');
ttZoomOut = findobj(tbh.Children,'Tag','Exploration.ZoomOut');
ttPan = findobj(tbh.Children,'Tag','Exploration.Pan');
ttDataCursor = findobj(tbh.Children,'Tag','Exploration.DataCursor');
delete(setdiff(tbh.Children,[ttZoomIn ttZoomOut ttPan ttDataCursor]))
I do not believe that there is currently a way to add buttons from the standard toolbar to a custom toolbar. I work for MathWorks, and I have forwarded this feature request to the appropriate team. It will be considered for inclusion in a future release of MATLAB.
I hope that this information helps!
-Cam