编辑的文章
这些是我使用你的模板创建的新函数,以帮助我解决问题。
代码本身没问题,但“检查光标”的框没有覆盖在坐标轴上。实际上它们离得很远。我使用了一次set(hAx, 'Units','pixels')和一次注释掉它的disp(axPos)。结果显示为:
1.
169.0000 71.0000 126.0000 51.0000
94.0000 122.0000 126.0000 51.0000
19.0000 71.0000 126.0000 51.0000
94.0000 20.0000 126.0000 51.0000
这是轴的GUIDE坐标,而不是正在显示的“Currentpoint”坐标。
2.
33.6000 5.3846 25.2000 3.9231
18.6000 9.3077 25.2000 3.9231
3.6000 5.3846 25.2000 3.9231
18.6000 1.4615 25.2000 3.9231
我不知道这些来自哪里,但它们更接近需要的位置,但是向左偏移了约60个像素。
以下是代码:
function HVACSM_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to HVACSM (see VARARGIN)
% Choose default command line output for HVACSM
handles.output = hObject;
NUM = 4;
imgOff1 = imread('cond.png');
imgOn1 = imread('condH.png');
imgOff2 = imread('comp.png');
imgOn2 = imread('compH.png');
imgOff3 = imread('evap.png');
imgOn3 = imread('evapH.png');
imgOff4 = imread('exp.png');
imgOn4 = imread('expH.png');
imgOff = cell(1,NUM);
imgOff{1} = imgOff1;
imgOff{2} = imgOff2;
imgOff{3} = imgOff3;
imgOff{4} = imgOff4;
imgOn = cell(1,NUM);
imgOn{1} = imgOn1;
imgOn{2} = imgOn2;
imgOn{3} = imgOn3;
imgOn{4} = imgOn4;
%# setup axes
hAx = zeros(1,NUM);
hImg = zeros(1,NUM);
hAx = [handles.axes1 handles.axes2 handles.axes3 handles.axes4];
hImg(1) = imagesc(imgOff{1}, 'Parent',hAx(1));
hImg(2) = imagesc(imgOff{2}, 'Parent',hAx(2));
hImg(3) = imagesc(imgOff{3}, 'Parent',hAx(3));
hImg(4) = imagesc(imgOff{4}, 'Parent',hAx(4));
set(hAx, 'XTick',[], 'YTick',[],'Box', 'on')
%# get corner-points of each axis
set(hAx, 'Units','pixels')
axPos = cell2mat( get(hAx,'Position') );
disp(axPos)
p = zeros(5,2,NUM);
for k=1:NUM
p(:,:,k) = bsxfun(@plus, axPos(k,1:2), ...
[0 0; axPos(k,3) 0; axPos(k,3:4); 0 axPos(k,4); 0 0]);
end
handles.p = p;
handles.hAx = hAx;
handles.hImg = hImg;
handles.imgOff = imgOff;
handles.imgOn = imgOn;
% Update handles structure
guidata(hObject, handles);
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
pos = get(hObject,'CurrentPoint');%CurrentPoint
posx = pos(1); posy = pos(2);
%Display to check to see if the position is working
posx = num2str(posx);
posy = num2str(posy);
set(handles.Xpos,'String',posx);
set(handles.Ypos,'String',posy);
p = handles.p;
hImg = handles.hImg;
hAx = handles.hAx;
imgOff = handles.imgOff;
imgOn = handles.imgOn;
%# for each axis, determine if we are inside it
for i=1:numel(hImg)
if inpolygon(pos(1),pos(2), p(:,1,i),p(:,2,i))
set(hImg(i), 'CData',imgOn{i})
set(hAx(i), 'LineWidth',3, 'XColor','r', 'YColor','r')
else
set(hImg(i), 'CData',imgOff{i})
set(hAx(i), 'LineWidth',1, 'XColor','k', 'YColor','k')
end
end
这些是使用原始代码“set(hAx,'Units','pixels')”的击中框。
注意*其他击中框偏离屏幕顶部右侧,或将如此。
这些是使用修改后的代码“set(hAx,'Units','characters')”的击中框。
注意*当代码被注释掉时,会发生完全相同的事情。
测试时间为8月2日。
.
.
.
补充说明 原帖
运行我的GUI之后,我遇到了这个错误[Fatal Error]:-1:-1: 文件过早结束。
这是在以下代码块中发生的:
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
pos = get(hObject,'CurrentPoint');
%CurrentPoint
posx = pos(1); posy = pos(2);
%Display to check to see if the position is working
posx = num2str(posx); posy = num2str(posy);
set(handles.Xpos,'String',posx); set(handles.Ypos,'String',posy);
%If mouse over then update the immage
if ((115 < pos(1)) && (pos(1) < 125) && (7 < pos(2)) && (pos(2) < 11))
axes(handles.axes1);
imshow('condH.png')
else
axes(handles.axes1);
imshow('cond.png')
end
if ((90 < pos(1)) && (pos(1) < 100) && (7 < pos(2)) && (pos(2) < 11))
axes(handles.axes2);
imshow('compH.png')
else
axes(handles.axes2);
imshow('comp.png')
end
if ((80 < pos(1)) && (pos(1) < 90) && (7 < pos(2)) && (pos(2) < 11))
axes(handles.axes3);
imshow('evapH.png')
else
axes(handles.axes3);
imshow('evap.png')
end
if ((90 < pos(1)) && (pos(1) < 100) && (2 < pos(2)) && (pos(2) < 5))
axes(handles.axes4);
imshow('expH.png')
else
axes(handles.axes4);
imshow('exp.png')
end
通常情况下,GUI 运行良好,直到我将鼠标悬停在预定框上触发其中一个 if 语句。然后 GUI 停止响应,并且直到重新启动 Matlab,它才能再次打开。
WindowButtonMotionFcn
回调,如上所示。更新图像的CData
比从头开始重新绘制它更快。最后,我使用了INPOLYGON
函数,但如果您喜欢,可以保留您拥有的测试... - AmroWindowButtonMotionFcn
时,它通过for循环遍历每个区域。 然而,您将4个子图存储在一个矩阵中,然后将4个属于子图的图像存储起来...我有预先制作的坐标轴 我能做类似hAx(1) = axes1; hAx(2) = axes2;这样的事情吗? - CMacDady