How to prevent uimenu (MATLAB) from disappearing when checking
I added uicontextmenu
lines to the object. uicontextmenu
includes 3 checkboxes. whenever I check any of them uicontextmenu
disappears. I want it to uicontextmenu
be displayed for a while so that I can check multiple cells and see the change (same as the button group but in uicontextmenu
). Is there any solution for this or another approach?
cmenu=uicontextmenu;
set(he,'uicontextmenu',cmenu);
item1=uimenu(cmenu,'label','Data A','checked','off','callback',@func_a);
item2=uimenu(cmenu,'label','Data B','checked','off','callback',@func_b);
item3=uimenu(cmenu,'label','Data C','checked','off','callback',@func_c);
basically, he
is a line object created by plot(x,y)
and func_a, func_b, func_c
is a function to convert a property 'checked'
to on|off
.
source to share
This example is heavily inspired by Benoit_11's solution, but slightly improved. I also got the impression that the three different functions in your callback were doing different things, so I made 3 different menus change different line properties (instead of changing the same property with different values).
I made a uimenu callback in one nested function. It decides what to do based on the parameter what2do
given in the uimenu definition (but feel free to keep 3 separate functions). Note, however, that the function that toggles the checkbox is the same for all uimenu (you don't need a separate function for each one).
function hf = TestUiContext2
%// Extension of Benoit_11 solution
clear ; clc ; close all
hf = figure ; %// return the handle of the figure
hax = axes; %// Create axes and save handle
plot(rand(20,3)); %// Plot three lines
hcmenu = uicontextmenu; %// Define a context menu; it is not attached to anything
%// Define the context menu items and install their callbacks
item1 = uimenu(hcmenu, 'Label','Bold line' , 'Callback' , {@uiCallback,'bold'} );
item2 = uimenu(hcmenu, 'Label','Dotted line' , 'Callback' , {@uiCallback,'dots'} );
item3 = uimenu(hcmenu, 'Label','Markers on' , 'Callback' , {@uiCallback,'mark'} );
hlines = findall(hax,'Type','line'); %// Locate line objects
for line = 1:length(hlines) %// Attach the context menu to each line
set(hlines(line),'uicontextmenu',hcmenu)
end
function uiCallback(obj,~,what2do)
hline = gco ;
switch what2do
case 'bold'
toggle_bold_line(hline)
case 'dots'
toggle_dotted_line(hline)
case 'mark'
toggle_markers(hline)
end
%// reposition the context menu and make it visible
set(hcmenu,'Position',get(gcf,'CurrentPoint'),'Visible','on')
toggle_checkmark(obj) %// toggle the checkmark
end
function toggle_checkmark(obj)
if strcmp(get(obj,'Checked'),'on')
set(obj,'Checked','off')
else
set(obj,'Checked','on')
end
end
function toggle_bold_line(hline)
if get(hline,'LineWidth')==0.5
set(hline,'LineWidth',2)
else
set(hline,'LineWidth',0.5)
end
end
function toggle_dotted_line(hline)
if strcmpi(get(hline,'LineStyle'),':')
set(hline,'LineStyle','-')
else
set(hline,'LineStyle',':')
end
end
function toggle_markers(hline)
if strcmpi(get(hline,'Marker'),'none')
set(hline,'Marker','o')
else
set(hline,'Marker','none')
end
end
end
Now you can enjoy ticking the entire menu in one go;)
source to share
Here's a workaround that might do the trick for you. It's not overly elegant, but it seems to work.
The trick is to set the parameter " Visible
" to " on
" in each callback (ie @func_a, @funct_b and @funct_c). When I run the following example (based on the demo on the Mathworks website), the menu doesn't disappear when the selection changes. Please note that I have created separate functions for each callback.
Here is the code:
function TestUiContext( ~)
%// Based on example from The Mathworks
%// http://www.mathworks.com/help/matlab/ref/uicontextmenu.html
clear
clc
close all
%// Create axes and save handle
hax = axes;
%// Plot three lines
plot(rand(20,3));
%// Define a context menu.
hcmenu = uicontextmenu;
%// Define the context menu items and install their callbacks
item1 = uimenu(hcmenu,'Label','dashed','Callback',@(s,e) hcb1);
item2 = uimenu(hcmenu,'Label','dotted','Callback',@(s,e) hcb2);
item3 = uimenu(hcmenu,'Label','solid','Callback',@(s,e) hcb3);
%// Locate line objects
hlines = findall(hax,'Type','line');
%// Attach the context menu to each line
for line = 1:length(hlines)
set(hlines(line),'uicontextmenu',hcmenu)
end
%// In the callback of every item/option, set the menu property 'Visible' to 'on'.
function hcb1
set(gco,'LineStyle','--');
set(hcmenu,'Visible','on')
end
function hcb2
set(gco,'LineStyle',':');
set(hcmenu,'Visible','on')
end
function hcb3
set(gco,'LineStyle','-');
set(hcmenu,'Visible','on')
end
end
And 2 screenshots to show what it looks like:
And moving the cursor down:
So, as I said, not perfect, but hopefully it does the job for you!
source to share