MATLAB Answers

0

UICONTEXTMENU will not appear on GUI

Lawson Hoover さんによって質問されました 2012 年 12 月 9 日
I have been trying to get a context menu to appear anywhere on the eniter figure, which also has a plot on it. I have this code:
cmenu = uicontextmenu('Parent',S.fh);
bgmenu = uimenu(cmenu,'label','Background');
bg1 = uimenu(bgmenu,'Label','Grid','CallBack',set(imread,'filename','grid.jpg'));
bg2 = uimenu(bgmenu,'Label','ConTrail','Callback',set(imread,'filename','grid.png'));
bg3 = uimenu(bgmenu,'Label','Opera','Callback',set(imread,'filename','Opera.jpg'));
The Menu will not show up at all.

  0 件のコメント

サインイン to comment.

1 件の回答

Login_Name
回答者: Login_Name
2012 年 12 月 9 日
編集済み: Login_Name
2012 年 12 月 9 日
 採用された回答

You need to set the uicontextmenu property, not assign a parent.
S.fh = figure;
cmenu = uicontextmenu;
bgmenu = uimenu(cmenu,'label','Background');
set(gcf,'uicontextmenu',cmenu)
Now right click inside the figure....

  17 件のコメント

Lawson Hoover 2012 年 12 月 9 日
Ok, I am able to change to images now but instead of it being the background it is, the graph in which I graph data. And Id rather not have to load the clown first to get it to work.
code:
ImAxes = axes('Parent',S.fh,'units','normalized','Position',[0 0 1 1],'HitTest','off');
I = imread('curtain.jpg');
Im = imagesc(I,'HitTest','off');
colormap gray
set(ImAxes,...
'handlevisibility','off', ...
'visible','off');
X = load('clown');
cmenu = uicontextmenu;
bgmenu = uimenu(cmenu,'label','Background');
S.um(1) = uimenu(bgmenu,'Label','Grid');
S.um(2) = uimenu(bgmenu,'Label','ConTrail');
S.um(3) = uimenu(bgmenu,'Label','Opera');
S.um(4) = uimenu(bgmenu,'Label','Light Wood');
set(S.um,'CallBack',@menu_call_bg);
IMG = ind2rgb(X.X,X.map);
S.IH = image(IMG);
set(S.IH,'uicontextmenu',cmenu)
guidata(S.fh,S)
function [] = menu_call_bg(varargin)
S = guidata(gcbf);
switch gcbo
case S.um(1)
I = imread('grid.jpg');
case S.um(2)
I = imread('ConTrail.png');
case S.um(3)
I = imread('Opera.jpg');
case S.um(3)
I = imread('leaf.jpg');
end
set(S.IH,'cdata',I)
end
Login_Name
2012 年 12 月 9 日
I think you need to slow down and think it through. You have all of the pieces to get this working but you haven't quite seen it yet. It seems like you are trying to rush through without understanding what is going on.
Why would you think you had to load clown every time to make your GUI work? I did that as an example because I don't know what images you have on your disk but I do know you have the sample images MATLAB came with!! Scroll up to the example I posted and look at the comments where I explain this clearly.
You need to make sure that S.IH is plotted on the correct axes the first time you create it. Then changing the cdata will not move it to another axes.
Lawson Hoover 2012 年 12 月 9 日
Ok so Im slowly getting it, you are loading clown and then saying it. But Clown is also a .mat right? so how would I save my images as a .mat? Also I have actually got the new image to appear on the back ground now, but instead of replacing it is just creating a new axes.

サインイン to comment.



Translated by