How can I assign a GIF to a pushbutton?

조회 수: 3 (최근 30일)
Zair Khan
Zair Khan 2018년 3월 15일
편집: Adam Danz 2020년 4월 28일
I need to assign a gif to a pushbutton in a MATLAB GUI. I am using GUIDE, and I have the gif. I would like to have the gif show on the pushbutton, then show an image displayed at random. Basically I have a gif of a dice rolling. I would like to hit the pushbutton, show the gif of the dice rolling, then any one of a die side. Thank you.

답변 (1개)

Tony Mohan Varghese
Tony Mohan Varghese 2018년 3월 21일
You cannot assign a gif image to a pushbutton using GUIDE. A true colour RGB image can be assigned to a push button.
To display an image on a GUI component, try these four steps:
1. Read in the image you want to be displayed on the component.
2. Make sure that it is a True Color Image.
3. Resize the image to fit on the component.
4. Create the component and set the 'cdata' property to be the image.
Below is a quick example of how to display an image on a pushbutton:
[x,map]=imread('ngc6543a.jpg');
I2=imresize(x, [42 113]);
h=uicontrol('style','pushbutton',...
'units','pixels',...
'position',[20 20 113+10 42+10],...
'cdata',I2)
If it is not a True color image, then you can use one of the conversion functions in the Image Processing Toolbox to convert it.
ind2rgb - Convert indexed image to RGB image.
isrgb - Return true for RGB image.
rgb2gray - Convert RGB image or colormap to grayscale.
rgb2ind - Convert RGB image to indexed image.
If you are using a GUIDE GUI and wish to modify an existing component that exists in the 'handles' structure, use the SET command to set the 'CDATA' property as shown below:
set(handles.pushbutton1,'cdata',I2);

카테고리

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