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 Center および File ExchangeConvert Image Type についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by