MATLAB Answers


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
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');
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.
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
'handlevisibility','off', ...
X = load('clown');
cmenu = uicontextmenu;
bgmenu = uimenu(cmenu,'label','Background'); = uimenu(bgmenu,'Label','Grid'); = uimenu(bgmenu,'Label','ConTrail'); = uimenu(bgmenu,'Label','Opera'); = uimenu(bgmenu,'Label','Light Wood');
IMG = ind2rgb(X.X,;
S.IH = image(IMG);
function [] = menu_call_bg(varargin)
S = guidata(gcbf);
switch gcbo
I = imread('grid.jpg');
I = imread('ConTrail.png');
I = imread('Opera.jpg');
I = imread('leaf.jpg');
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