Show image in Button using code

1 Ansicht (letzte 30 Tage)
Khalala Mamouri
Khalala Mamouri am 25 Sep. 2020
Kommentiert: Khalala Mamouri am 25 Sep. 2020
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 Kommentare
Mario Malic
Mario Malic am 25 Sep. 2020
Please move/paste your answer to answers section and accept it. Thanks.
Khalala Mamouri
Khalala Mamouri am 25 Sep. 2020
Thank you Mario for the information
have a nice day
lakhdar

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Khalala Mamouri
Khalala Mamouri am 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 !

Weitere Antworten (0)

Kategorien

Mehr zu Convert Image Type finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by