Show image in Button using code

1 次查看(过去 30 天)
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