make image pop up when button group is pressed in matlab

One way to show an image when a button is pressed is to use the uicontrol function to create a button and imshow to display the image in a separate axis. Here's an example code snippet that creates a button group and displays an image when a button is pressed:

main.m
% define image file path
img_path = 'path/to/image.jpg';

% create figure and axis for displaying image
fig = figure();
ax = axes('Parent', fig, 'Position', [0.1 0.3 0.8 0.6]);

% create button group
btn_group = uibuttongroup('Parent', fig, 'Position', [0.1 0.1 0.8 0.2]);

% create buttons in the group
btn1 = uicontrol('Parent', btn_group, 'Style', 'radiobutton', ...
    'String', 'Show Image', 'Position', [50 30 100 20]);
btn2 = uicontrol('Parent', btn_group, 'Style', 'radiobutton', ...
    'String', 'Hide Image', 'Position', [200 30 100 20]);

% set callback function for buttons
set(btn1, 'Callback', @show_image);
set(btn2, 'Callback', @hide_image);

% define callback functions
function show_image(src, event)
    % read image file
    img = imread(img_path);
    % display image in axis
    imshow(img, 'Parent', ax);
end

function hide_image(src, event)
    % clear axis
    cla(ax);
end
901 chars
33 lines

In this example, a button group is created with two radio buttons (btn1 and btn2). The show_image and hide_image functions are set as the callback functions for the buttons using the set function. When btn1 is pressed, the show_image function reads the image file and displays it in the ax axis using the imshow function. When btn2 is pressed, the hide_image function clears the ax axis using the cla function.

related categories

gistlibby LogSnag