現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
UICONTEXTMENU will not appear on GUI
11 ビュー (過去 30 日間)
古いコメントを表示
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.
採用された回答
Matt Fig
2012 年 12 月 9 日
編集済み: Matt Fig
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 日
It still wont work, maybe the way I set the back ground has to do with it. I set the background image with:
ImAxes = axes('Parent',S.fh,'units','normalized','Position',[0 0 1 1]);
I = imread('light wood.jpg');
Im = imagesc(I);
colormap gray
set(ImAxes,...
'handlevisibility','off', ...
'visible','off');
Lawson Hoover
2012 年 12 月 9 日
You are the man! Thank you! Also how would I change what image is load when a selection is made?
Lawson Hoover
2012 年 12 月 9 日
I did that but now When I set the call back to this:
bg1 = uimenu(bgmenu,'Label','Grid','Callback',set(Im,'CData','grid.jpg'));
The first level of the context menu shows up but then nothing else shows up. I think I am setting up the callback wrong.
Matt Fig
2012 年 12 月 9 日
編集済み: Matt Fig
2012 年 12 月 9 日
To clear up the previous point.... Setting the hittest to off makes it so that when you click on the axes or image the object that is selected is whatever is below those two (i.e., they cannot become the selected object), which is the figure. Otherwise you would need to set the uicontextmenu of those objects rather than the figure like you showed in your OP.
Lawson Hoover
2012 年 12 月 9 日
Ok, so when I turn the HitTest off, I wont allow me to change the file name on the back ground image? I understand that when it is off It is essentially clicking the back most figure. But why cant I change the file name for the background image?
Matt Fig
2012 年 12 月 9 日
Show the code. Nobody can know what you are doing or why MATLAB isn't giving you what you expect if we don't see the code. Are you still setting the callback to this:
set(imread,'filename','grid.jpg')
I can't believe that won't error, unless you have masked the IMREAD function with a variable!
Lawson Hoover
2012 年 12 月 9 日
Setting up background image:
ImAxes = axes('Parent',S.fh,'units','normalized','Position',[0 0 1 1],'HitTest','off');
%uistack(ImAxes,'bottom');
I = imread('ConTrail.png');
Im = imagesc(I,'HitTest','off');
colormap gray
set(ImAxes,...
'handlevisibility','off', ...
'visible','off');
and then the associating context menu code.
cmenu = uicontextmenu;
bgmenu = uimenu(cmenu,'label','Background');
set(gcf,'uicontextmenu',cmenu)
bg1 = uimenu(bgmenu,'Label','Grid','Callback',set(Im,'CData','grid.jpg'));
bg2 = uimenu(bgmenu,'Label','ConTrail');
bg3 = uimenu(bgmenu,'Label','Opera');
bg4 = uimenu(bgmenu,'Label','Light Wood');
If you need more I can just load the first whole section of the code. That creates the GUI
Matt Fig
2012 年 12 月 9 日
編集済み: Matt Fig
2012 年 12 月 9 日
I don't think you are programming callbacks correctly. Are you telling me that code doesn't produce an error when you run it? Your callback is a function that will run when the callback is invoked.
Here is a short and simple example of how to load different images and set them depending on the results of a context menu. Notice how the callback is set and used.
function [] = gui_image_change()
S.fh = figure('units','pixels',...
'position',[400 400 480 300],...
'menubar','none',...
'name','Image_GUI',...
'resize','off',...
'numbertitle','off');
S.ax = axes('units','pixels',...
'position',[0 0 480 300],...
'xtick',[],'ytick',[]);
X = load('clown'); % This is a built-in ML example.
IMG = ind2rgb(X.X,X.map); % We want to convert it to RGB.
imwrite(IMG,'myclown_IMG.jpg','jpg'); % Save to disk.
S.IH = image(IMG); % Display the image.
% Save variants to disk. In practice these
% would already be on disk so you could
% skip this step and just use the names in
% the callback.
I = IMG; I(:,:,1) = .8;
imwrite(I,'myclown_red_IMG.jpg','jpg');
I = IMG; I(:,:,2) = .8;
imwrite(I,'myclown_green_IMG.jpg','jpg');
I = IMG; I(:,:,3) = .8;
imwrite(I,'myclown_blue_IMG.jpg','jpg');
S.UCM = uicontextmenu;
S.UM(1) = uimenu(S.UCM,'Label','Plain');
S.UM(2) = uimenu(S.UCM,'Label','Red');
S.UM(3) = uimenu(S.UCM,'Label','Green');
S.UM(4) = uimenu(S.UCM,'Label','Blue');
set(S.IH,'uicontextm',S.UCM)
set(S.UM,'CallBack',@menu_call);
guidata(S.fh,S) % Save the structure for later use.
function [] = menu_call(varargin)
% Callbacks for the menus
S = guidata(gcbf); % Get the structure
switch gcbo
case S.UM(1)
I = imread('myclown_IMG.jpg');
case S.UM(2)
I = imread('myclown_red_IMG.jpg');
case S.UM(3)
I = imread('myclown_green_IMG.jpg');
case S.UM(4)
I = imread('myclown_blue_IMG.jpg');
otherwise
disp('Twilight Zone!')
end
set(S.IH,'cdata',I)
Lawson Hoover
2012 年 12 月 9 日
Ok so I read it over and created a dedicated call back but now It is saying:
Attempt to reference field of non-structure array.
Error in Beam_Deflection_GUI_3>menu_call (line 342)
case S.um(1)
Error while evaluating uimenu Callback
I used the same set up code to create the background image and made the uimenu this:
cmenu = uicontextmenu; bgmenu = uimenu(cmenu,'label','Background'); set(gcf,'uicontextmenu',cmenu)
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);
function [I] = menu_call(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('light wood.jpg');
end
set(Im,'cdata',I)
end
Lawson Hoover
2012 年 12 月 9 日
Ok fixed that missed point, but now I get the error: Undefined function or variable 'Im'.
Error in Beam_Deflection_GUI_3>menu_call (line 352)
set(Im,'cdata',I)
Error while evaluating uimenu Callback
That variable is created in the function above the call back with:
ImAxes = axes('Parent',S.fh,'units','normalized','Position',[0 0 1 1],'HitTest','off');
I = imread('ConTrail.png');
Im = imagesc(I,'HitTest','off');
colormap gray
set(ImAxes,...
'handlevisibility','off', ...
'visible','off');
And I stop the error by making the variable global but when that happens, the background image does not change, and it does not yield any errors.
Matt Fig
2012 年 12 月 9 日
編集済み: Matt Fig
2012 年 12 月 9 日
But a subfunction has no access to variables created in the main function. Otherwise we wouldn't need to save S in guidata in the first place, then recall it in the subfunction! So you will notice that I saved the image handle in structure S as S.IH, like all data I will need in this callback or any other.
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
Matt Fig
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 日
編集済み: 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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Interactive Control and Callbacks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)