已编辑的帖子
这些是我使用您的模板创建的新功能来帮助我。 发生的事情虽然代码工作正常,但是“检查光标”框并不在轴的顶部。事实上,他们离我很远。我用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
这些是hitboxes与原始代码“set(hAx,'Units','pixels')”
注意*其他人离屏幕右上方,或者是。
这些是具有更改代码“set(hAx,'Units','characters')”的命中框“
注意*这与注释掉代码时完全相同。
8月2日测试
附录 原帖
我在运行GUI [致命错误]后收到此错误:-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重新启动之前不会再次打开。
答案 0 :(得分:2)
很难说在没有看到整个代码的情况下为什么MATLAB会崩溃。出于这个原因,我在下面写了一个简短的例子。它说明了如何使用rollover effect回调编写模拟WindowButtonMotionFcn的GUI:
function testRolloverGUI()
%# prepare rollover image
imgOff = imread('coins.png');
imgOn = imcomplement(imgOff);
%# setup figure
hFig = figure('Resize','off', 'MenuBar','none', 'Color','w');
set(hFig, 'WindowButtonMotionFcn',@figWindowButtonMotionFcn);
hTxt = uicontrol('Style','text', 'String','(0,0)');
%# setup axes
NUM = 4;
hAx = zeros(1,NUM);
hImg = zeros(1,NUM);
for k=1:NUM
hAx(k) = subplot(2,2,k);
hImg(k) = imagesc(imgOff, 'Parent',hAx(k));
end
colormap(gray)
set(hAx, 'XTick',[], 'YTick',[], 'Box','on')
%# get corner-points of each axis
set(hAx, 'Units','pixels')
axPos = cell2mat( get(hAx,'Position') );
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
%# callback function
function figWindowButtonMotionFcn(hObj,ev)
%# get mouse current position
pos = get(hObj, 'CurrentPoint');
set(hTxt, 'String',sprintf('(%g,%g)',pos))
%# 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)
set(hAx(i), 'LineWidth',3, 'XColor','r', 'YColor','r')
else
set(hImg(i), 'CData',imgOff)
set(hAx(i), 'LineWidth',1, 'XColor','k', 'YColor','k')
end
end
end
end
为了回应您的评论,我在GUIDE中重新创建了示例。以下是主要部分:
%# --- Executes just before rollover is made visible.
function rollover_OpeningFcn(hObject, eventdata, handles, varargin)
%# Choose default command line output for rollover
handles.output = hObject;
%# allocate
NUM = 4;
imgOff = cell(1,NUM);
imgOn = cell(1,NUM);
hImg = zeros(1,NUM);
%# read images
imgOff{1} = imread('coins.png');
imgOn{1} = imcomplement(imread('coins.png'));
imgOff{2} = imread('coins.png');
imgOn{2} = imcomplement(imread('coins.png'));
imgOff{3} = imread('coins.png');
imgOn{3} = imcomplement(imread('coins.png'));
imgOff{4} = imread('coins.png');
imgOn{4} = imcomplement(imread('coins.png'));
%# setup axes
hAx = [handles.axes1 handles.axes2 handles.axes3 handles.axes4];
for i=1:NUM
hImg(i) = imagesc(imgOff{i}, 'Parent',hAx(i));
end
colormap(hObject, 'gray')
set(hAx, 'XTick',[], 'YTick',[], 'Box','on')
%# make sure axes units match that of the figure
set(hAx, 'Units',get(hObject, 'Units'))
%# check axes parent container (figure or panel)
hAxParents = cell2mat( get(hAx,'Parent') );
idx = ismember(get(hAxParents,'Type'), 'uipanel');
ppos = cell2mat( get(hAxParents(idx), 'Position') );
%# adjust position relative to parent container
axPos = cell2mat( get(hAx,'Position') );
axPos(idx,1:2) = axPos(idx,1:2) + ppos(:,1:2);
%# compute corner-points of each axis
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
%# store in handles structure
handles.p = p;
handles.hAx = hAx;
handles.hImg = hImg;
handles.imgOff = imgOff;
handles.imgOn = imgOn;
%# Update handles structure
guidata(hObject, handles);
%# --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
%# CurrentPoint
pos = get(hObject,'CurrentPoint');
set(handles.text1,'String',sprintf('(%g,%g)',pos));
%# for each axis, determine if we are inside it
for i=1:numel(handles.hImg)
if inpolygon(pos(1),pos(2), handles.p(:,1,i),handles.p(:,2,i))
set(handles.hImg(i), 'CData',handles.imgOn{i})
set(handles.hAx(i), 'LineWidth',3, 'XColor','r', 'YColor','r')
else
set(handles.hImg(i), 'CData',handles.imgOff{i})
set(handles.hAx(i), 'LineWidth',1, 'XColor','k', 'YColor','k')
end
end
GUI与以前的组件基本相同,只是轴包含在uipanel
内(类似于GUI的屏幕截图):
有几点需要注意:
由于我们的目标是将图形的CurrentPoint
与轴位置进行比较,因此它们与图的Units
具有相同的set(hAx, 'Units',get(hObject, 'Units'))
非常重要,因此:Position
根据documentation,轴axPos(idx,1:2) = axPos(idx,1:2) + ppos(:,1:2);
属性相对于其父容器,并且因为四个轴位于面板内,我们需要相应地调整它们的位置:{{ 1}}