Custom 'uimenu' concatenates each time GUI is run

조회 수: 5 (최근 30일)
TJ
TJ 2013년 1월 29일
답변: Kris 2014년 11월 20일
I've just started working on writing GUIs without using GUIDE. Each time I run my GUI function that has a custom dropdown uimenu (i.e.[File Edit Search], the uimenu keeps concatenating onto the figure unless I close the figure window out completely.
For instance, after running the function twice, the menu bar reads [File Edit Search File Edit Search].
I have tried setting the 'menubar' property to 'none', and I've tried clearing the uimenu handle at the beginning of the function. Nothing seems to work.
Here is some example code taken from the mathworks website. If you drop this into the editor and run it a few times without closing the figure, you'll end up with a figure with multiple finds in the menu bar. Thanks in advance.
f = figure(22);
set(f, 'MenuBar','None');
mh = uimenu(f,'Label','Find');
frh = uimenu(mh,'Label','Find and Replace ',...
'Callback','goto',...
'Accelerator', 'Q');
frh = uimenu(mh,'Label','Variable','Separator', 'on' );
uimenu(frh,'Label','Name', ...
'Callback','variable');

답변 (2개)

Sean de Wolski
Sean de Wolski 2013년 1월 29일
Every time you call:
mh = uimenu(f,'Label','Find');
It adds another menu. If you instead want to overwrite or delete the existing menu either delete() it or set() its properties to be something else.
f = figure(22);
set(f, 'MenuBar','None');
if exist('mh','var') %if it's there, delete it.
delete(mh);
end
mh = uimenu(f,'Label','Find');
frh = uimenu(mh,'Label','Find and Replace ',...
'Callback','goto',...
'Accelerator', 'Q');
frh = uimenu(mh,'Label','Variable','Separator', 'on' );
uimenu(frh,'Label','Name', ...
'Callback','variable');

Kris
Kris 2014년 11월 20일
It is quite simple like this:
existingmenus = findall( hfig, 'Type', 'uimenu' );
arrayfun(@(x) delete(x), existingmenus);
Cheers,
K

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by