how to display another same figure when i click the image inside axis1 in gui?it doesn't response

function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set([handles.axes1], 'buttondownfcn', @show_in_figure)
a=axes(handles.axes1);
set(a, 'hittest', 'off');
function show_in_figure(hObject, event)
hfig = figure();
hax = axes('Parent',hfig);
copyobj(hObject',hax);
axes(hax, 'image');
end

Answers (1)

Hi,
I understand that you want to display same figure in another window when clicked on an image on the GUI.
I have created a GUI window to show the image. I have passed the image object to Callback function which handles “ButtonDownFcn” event.
Here is the sample code which display same figure in another window when clicked on an image on the GUI:
function myGUI
% Create the GUI figure
handles.figure1 = figure('Position',[100 100 400 300]);
% Load the image
handles.image = imread('your_image.jpg'); % Replace 'your_image.jpg' with your actual image file
% Create a new axes object
handles.axes1 = axes('Parent', handles.figure1, 'Position',[0.1 0.1 0.8 0.8]);
% Display the image
handles.imageObj = image(handles.image, 'Parent', handles.axes1);
% Set the ButtonDownFcn for the image object
set(handles.imageObj, 'ButtonDownFcn', {@imageObj_ButtonDownFcn, handles});
% Store the handles structure
guidata(handles.figure1, handles);
end
function imageObj_ButtonDownFcn(hObject, eventdata, handles)
disp('Clicked on the image!');
% Create a new figure
hfig = figure('Position',[100 100 400 300]);
% Create a new axes object in the new figure
hax = axes('Parent', hfig, 'Position',[0.1 0.1 0.8 0.8]);
% Display the modified image in the new axes
modifiedImage = imresize(handles.image, 0.5); % Modify the image as per your requirement
image(modifiedImage, 'Parent', hax);
axis off;
end
Run the code by calling the ''myGUI" function from another script.
% Call the GUI function to start the application
myGUI();
Refer to the documentation regarding passing arguments to the Callbacks for more information. Section “Create Callback with Additional Input Arguments” of https://www.mathworks.com/help/matlab/creating_plots/create-callbacks-for-graphics-objects.html

Products

Release

R2021a

Asked:

on 31 Oct 2022

Answered:

on 30 Aug 2023

Community Treasure Hunt

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

Start Hunting!