Skip to content

Instantly share code, notes, and snippets.

@tabedzki
Created March 20, 2024 15:28
Show Gist options
  • Save tabedzki/517436a79f04d35a0922aa906e35260b to your computer and use it in GitHub Desktop.
Save tabedzki/517436a79f04d35a0922aa906e35260b to your computer and use it in GitHub Desktop.
This file serves as an example of a working dropdown menu in MATLAB to replace the deprecated uisplittools
% Create a figure and add a toolbar to it
fig = figure('Toolbar', 'none', 'MenuBar', 'none', 'Name', 'Custom Toolbar Dropdown', 'WindowButtonDownFcn', @(src, event)hideContextMenu(src));
tb = uitoolbar(fig);
% Load the icon (e.g. greenarrowicon)
icon = fullfile(matlabroot,'/toolbox/matlab/icons/greenarrowicon.gif');
[cdata,map] = imread(icon);
% Convert white pixels into a transparent background
map(map(:, 1) + map(:, 2) + map(:, 3) == 3) = NaN;
% Convert into 3D RGB-space
cdataRedo = ind2rgb(cdata,map);
% Create a blank image of the same size as the original image with all values set to NaN
arrowImg = NaN(size(cdataRedo));
% Draw a black triangle in the middle of the image
arrowHeight = size(cdataRedo, 1)/4; % Height of the arrow is 1/4 the height of the original image
arrowWidth = size(cdataRedo, 2); % Width of the arrow is the same as the width of the original image
startRow = size(cdataRedo, 1)/2 - arrowHeight/2; % Start drawing the triangle from the middle of the image
for i = 1:arrowHeight
arrowImg(startRow+arrowHeight-i, arrowWidth/2-i+1:arrowWidth/2+i-1) = 0; % Draw each row of the triangle
end
% Append the arrow image to the right of the original image
cdataRedo = horzcat(cdataRedo, arrowImg);
% Create a push tool in the toolbar
pt = uipushtool(tb,'cdata',cdataRedo,'TooltipString','Dropdown Menu', 'Separator', 'on');
% Set the callback for the click on image-icon
pt.ClickedCallback = @(src, event)dropdownClicked(src, event, fig);
function dropdownClicked(~, ~, fig, flag)
% Define items for the dropdown
items = {'Movement Function', 'Transformation Function', 'Experiment Code'};
% Create a context menu (acts as dropdown)
cm = uicontextmenu(fig);
% Populate the context menu with items
for i = 1:length(items)
% Different callback function can be made for each option of
% the context menu. Here, selction option is printed
uimenu(cm, 'Label', items{i}, 'Callback', @(s,e)disp(['Selected Option: ', items{i}]));
end
% Sample position of the context-menu
dropdownPos = [15, 420];
cm.Position = dropdownPos;
% Make the context menu visible
cm.Visible = 'on';
end
function hideContextMenu(src)
% Hide the context menu by setting the figure's UIContextMenu to none
src.UIContextMenu = [];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment