Show image in Button using code

조회 수: 1 (최근 30일)
Khalala Mamouri
Khalala Mamouri 2020년 9월 25일
댓글: Khalala Mamouri 2020년 9월 25일
Hi,
I want to make an interactive Panel, where the user clicks on (left or right ) side to add new Capacitors, or inductors to a database. The problem is that i want each button to have an image of electrical component (eather capacitor or inductor). can anyone help me please to impliment the image in the button? This is my code.
% Create a figure
fh = figure(1);
buttonPanelLeft = uipanel(fh, 'Position', [0.5 0 0.5 1], 'BackgroundColor', [0.2 1 .8]); % Create a different panel to hold the button
buttonPanelRight = uipanel(fh, 'Position', [0 0 0.5 1], 'BackgroundColor', [0.2 1 .8]); % Create a different panel to hold the button
% Create pushbuttons
leftButton = uicontrol(buttonPanelLeft, 'Style', 'pushbutton', ...
'String', 'Add New Capacitor', 'Units', 'normalized', 'Position', [0 0 1 1], ...
'Callback', @(src, event) buttonPressCallback('A'));
rightButton = uicontrol(buttonPanelRight, 'Style', 'pushbutton', ...
'String', 'Add New Inductor', 'Units', 'normalized', 'Position', [0 0 1 1], ...
'Callback', @(src, event) buttonPressCallback('B'));
% imshow("All_Logos.png",'Parent',rightButton'); NOT SURE HOW TO MAKE THE IMAGES SHOW ON THE BUTTON
% imshow("Filtring_Capacitor.png",'Parent',leftButton'); NOT SURE HOW TO MAKE THE IMAGES SHOW ON THE BUTTON
% Executes a particular code for each option
function buttonPressCallback(thisAxis)
switch thisAxis
case 'A'
close all
disp('capacitor')
case 'B'
close all
disp('Inductor')
end
end
  댓글 수: 2
Mario Malic
Mario Malic 2020년 9월 25일
Please move/paste your answer to answers section and accept it. Thanks.
Khalala Mamouri
Khalala Mamouri 2020년 9월 25일
Thank you Mario for the information
have a nice day
lakhdar

댓글을 달려면 로그인하십시오.

채택된 답변

Khalala Mamouri
Khalala Mamouri 2020년 9월 25일
I found the solution !
This is what you can do :
[x,map]=imread('All_Logos.png'); % Read image and store it in x
I2=imresize(x, [100 113]); % Resize image (Just an option)
% Create pushbuttons
leftButton = uicontrol(buttonPanelLeft, 'Style', 'pushbutton', ...
'String', 'Add New Capacitor', 'Units', 'normalized', 'Position', [0 0 1 1], ...
'Callback', @(src, event) buttonPressCallback('A'),'cdata',I2); % Use cdata to affect the
% The image data stored in I2
Done !

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by