|
% This function hopefully implements what you want to do for the most part
function h = moveImageTest
h.figure = figure;
h.axes(1) = axes('Parent', h.figure, ...
'Position', [0.05 0.1 0.4 0.4], ...
'XTick', [], ...
'YTick', [], ...
'XColor', get(h.figure, 'Color'), ...
'YColor', get(h.figure, 'Color'), ...
'ButtonDownFcn', @axes_moveImage, ...
'CreateFcn', {@axes_loadImage, 1});
h.axes(2) = axes('Parent', h.figure, ...
'Position', [0.55 0.1 0.4 0.4], ...
'XTick', [], ...
'YTick', [], ...
'XColor', get(h.figure, 'Color'), ...
'YColor', get(h.figure, 'Color'), ...
'CreateFcn', {@axes_loadImage, 2});
h.axes(3) = axes('Parent', h.figure, ...
'Position', [0.75 0.25 0.025 0.025], ...
'XTick', [], ...
'YTick', [], ...
'CreateFcn', @axes_loadImage3);
% Get handles saved in CreateFcn's
h2 = guidata(h.figure);
% Merge h and h2
fieldName = fieldnames(h2);
for i=1:length(h2)
h.(fieldName{i}) = h2.(fieldName{i});
end
guidata(h.figure, h);
function axes_loadImage(hObject, eventdata, axesNum)
h = guidata(hObject);
axes(hObject);
I = imread('rice.png');
% Use low level function call for image so axes properties are not reset
h.image(axesNum) = image('CData', I);
% Set HitTest property to off so button clicks will be handled by the
% parent axes
set(h.image(axesNum), 'HitTest', 'off');
axis tight;
hf = ancestor(hObject, 'Figure');
guidata(hf, h);
function axes_loadImage3(hObject, eventdata)
h = guidata(hObject);
axes(hObject);
I = zeros(20,20,3); %small black square
% Use low level function call for image so axes properties are not reset
h.image(3) = image('CData', I);
axis tight;
hf = ancestor(hObject, 'Figure');
guidata(hf, h);
function axes_moveImage(hObject, eventdata)
h = guidata(hObject);
AxesXLim = get(hObject, 'XLim');
AxesYLim = get(hObject, 'YLim');
Axes2Pos = get(h.axes(2), 'Position');
% Cursor position is in axes data units
% Get cursor position in normalized units
CursorPosition = get(hObject, 'CurrentPoint'); %in axes data units
CursorPositionN = [CursorPosition(1, 1)/AxesXLim(2) CursorPosition(1, 2)/AxesYLim(2)];
% Use cursor position (within axes 1) to find same relative position in
% axes 2
Axes3Pos = get(h.axes(3), 'Position');
NewAxes3Pos(1:2) = CursorPositionN.*Axes2Pos(3:4) + Axes2Pos(1:2);
NewAxes3Pos(3:4) = Axes3Pos(3:4);
set(h.axes(3), 'Position', NewAxes3Pos);
|