How I make a pushbutton invisible in a GUI?

22 次查看(过去 30 天)
I has a picture of a drumset/percussion and wants to click a drum which then makes a sound. So I wants a button on my drum, but invisible. So I can still see the drum on my window figure without pushbutton.
My Script:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[s,fs]=wavread('filename.wav');
sound(s,fs);
Thankyou..

回答(2 个)

Stephen23
Stephen23 2016-9-23
编辑:Stephen23 2016-9-23
Setting the Visible property to off will not work, because then the uicontrol does not respond to clicking.
Here are two alternatives that actually work, but are perhaps not as beautiful as an invisible, clickable button.
Button on top of an image
X = imread('drum.jpg');
fgh = figure('Color','white');
image(X)
axis off
axis image
uih = uicontrol(fgh,'Style','pushbutton', 'BackGroundColor','white',...
'Units','normalized','Position',[0.5,0.4,0.2,0.2], 'String','press',...
'Callback',@(h,e)disp('bang!'));
Button displaying an image
X = imread('drum.jpg');
fgh = figure('Color','white');
uih = uicontrol(fgh,'Style','pushbutton', 'BackGroundColor','white',...
'Units','pixels', 'Position',[20,20,200,200], 'String','press',...
'CData',X, 'Callback',@(h,e)disp('bang!'));
Both methods use this image:
  6 个评论
Stephen23
Stephen23 2016-9-23
编辑:Stephen23 2018-3-21
I don't see any simple solution that makes the button invisible. So far I have shown you:
  1. making button invisible: does not work.
  2. button on top of image: works.
  3. button contains image: works.
One more option would be to write your own "button" by defining a callback that detects the mouse position and decides how to respond based on its position. For this you will have to start by reading these:
then you will have to use some kind of indexing to specify the required location, and define a callback function that uses this to trigger your code.
Stephen23
Stephen23 2016-9-23
编辑:Stephen23 2018-3-21
"How i do that?" I showed you already. This is the second method in my answer. I even tested it and showed you a screenshot.

请先登录,再进行评论。


Amanda Irving
Amanda Irving 2019-8-8
Starting in R2019a, there is a new component available called uiimage.
You can assign an image using the ImageSource property and assign a callback using the ImageClickedFcn:

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by