set current axis to plot in several subplots

조회 수: 10 (최근 30일)
Jahandar Jahanipour
Jahandar Jahanipour 2017년 4월 28일
답변: Jahandar Jahanipour 2017년 5월 1일
I have a figure with two subplots I have defined the subplots as:
figure_handle = figure('Tag', 'main_figure');
right_hanlde = subplot(121,...
'Tag','right_plot',...
'Units', 'normalized');
left_handle = subplot(122,...
'Tag', 'left_plot',...
'Units','normalized',...
'Position',[.51 0.03 .496 .917]);
I have a pushbutton that plots my image.
uicontrol('style', 'pushbutton',...
'Units', 'normalized',...
'Position', [.56 .88 .02 .02],...
'String','plot',...
'Callback',@plot_Callback)
function plot_callback(hObject,~)
imshow (my_image);
end
The problem is that if I click anywhere in the right subplot, the current axis is going to be set on the right plot and if I push the plot pushbuttom, it shows my image on the right subplot.
I want to have a line of code in my callback function before "imshow" to set the axis on the right_plot.
Also, the subplot(122) does not work in my case because I have several other properties that by calling subplot() they are all going to be reset.
Thanks,

채택된 답변

Jahandar Jahanipour
Jahandar Jahanipour 2017년 5월 1일
I found the answer, in case someone else needs to use:
first, in the gui, we have to save the handles in the guidata (in case someone is not familiar with gui data - help guidata)
handles.left_handle = left_handle; % store the wanted handle in the handles sturcture
handles.right_handle = right_handle; % store the wanted handle in the handles sturcture
guidata(figure_handle,handles) % save all the handles in the guidata of the figure
in the callback function we can easily set the current axes of the current figure to the handle that we want:
function Callback_function(hObject,~)
handles = guidata(hObject) % call all the handles that we saved
set(gcf,'CurrentAxes',handles.left_handle) % set the current axis of the current figure to the handle that we want
imshow(...) % show the image that we want
end

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 4월 29일
imshow(my_image, 'Parent', left_handle)
(you would have to arrange for left_handle to be accessible in your callback.)
  댓글 수: 1
Jahandar Jahanipour
Jahandar Jahanipour 2017년 5월 1일
I pass the left_handle to the guidata and it is accessible in my callback. But I get the following error:
Error using imshow>validateParent (line 352) HAX must be a valid axes handle.
Error in imshow (line 251) validateParent(specific_args.Parent)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by