Show image in Button using code

2 views (last 30 days)
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 Comments
Mario Malic
Mario Malic on 25 Sep 2020
Please move/paste your answer to answers section and accept it. Thanks.
Khalala Mamouri
Khalala Mamouri on 25 Sep 2020
Thank you Mario for the information
have a nice day
lakhdar

Sign in to comment.

Accepted Answer

Khalala Mamouri
Khalala Mamouri on 25 Sep 2020
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 !

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!