Clear Filters
Clear Filters

how to add a separator line in popup uicontrol?

5 views (last 30 days)
Is there a way to add a separator line for a popup uicontrol, like what "Separator" keyword does for uimenu?
I searched, but did not get useful information. Did I search wrong keyword?
-Xiangrui Li

Answers (2)

Walter Roberson
Walter Roberson on 25 Dec 2015
uicontrol('style','popup') do not support separator lines. You can add an entry which is a bunch of dashes but that will affect the count of the entries.
There is a trick you might be interested in: each individual cell in the cell array of strings will be parsed as HTML if it begins with '<HTML>'. For example,
uicontrol('string', {'hello', 'ab cd', '<HTML>ab cd', '<HTML>ab<br>cdef', '<HTML>ab<br>--------', 'pqrs'})
the first nbsp will be treated as pure text because it is not within an HTML marker; the one on the next line will be. The '<br>' works on the line after that leading to the '<br>' followed by a line of dashes to allow a visual separator. The drawback is that when the item is chosen, the dashes will show up as part of the current selection.
  1 Comment
Xiangrui Li
Xiangrui Li on 26 Dec 2015
It is an interesting trick, although it doesn't look perfect. It is pity that matlab does not support this. I know one program (Igor) simply uses '-' as separator, but it will take a count of entries. Thank you for answering.

Sign in to comment.

Yvan Lengwiler
Yvan Lengwiler on 12 Jul 2016
Edited: Yvan Lengwiler on 14 Jul 2016
Here's a solution to this problem.
function MyGUI
hline = '--------------------------------'; % string for a horiz line
mPos = 1; % store position in menu
hMenu = uicontrol(... % create popup menu
'Style' ,'popupmenu',...
'String' ,{'item 1','item 2',hline,'item 3'},...
'Callback' ,@MenuItemChanged);
function MenuItemChanged(varargin) % callback of menu control
if strcmp(hMenu.String(hMenu.Value),hline) % user picked the hline?
hMenu.Value = mPos; % then undo this choice
mPos = hMenu.Value; % otherwise store the new choice
mPos stores the previous legitimate (non-separator) item chosen in the menu. If the user selects a separator line, the callback function overrides this and selects the previous choice. The process is sufficiently such that the user does not see that the separator is selected and then immediately unselected again.


Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!