Fatal error given by Matlab GUI
Edited Post
These are the new functions that I have created using your template to help me out. What is happening is though the code works fine, the 'check for cursor' boxes are not over top of the axes. In fact they are very far off. I used disp(axPos) once with set(hAx, 'Units','pixels'), and once with it commented out. It displayed:
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
Which is the GUIDE co ordinates of the axis but not the co ordinates that are being displayed for '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
I do not know where these come from but are closer to where they need to be but are ~60 pixels to the left.
Here is the code:
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
These are the hitboxes is with the original code "set(hAx, 'Units','pixels')"
Note* the others are way off the screen to the top right, or would be.
These are the hitboxes with the altered code "set(hAx, 'Units','characters')"Note* This is the exact same thing that happens when the code is commented out.
Tested Aug 2 .
.
.
.
Addendum Original Post
I am getting this error after running my GUI [Fatal Error] :-1:-1: Premature end of file.
It happens during this block of code:
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
Normally the GUI runs fine until I trigger one of the i开发者_如何学运维f statements by mousing over the predetermined box. Then the GUI stops responding and will not open again until Matlab is restarted.
It is difficult to tell why MATLAB is crashing without seeing your entire code. For this reason, I wrote the short example below. It illustrates how I would to write a GUI that simulates the rollover effect using the WindowButtonMotionFcn callback:
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
EDIT#2
In response to your comments, I recreated the example in GUIDE. Here are the main parts:
%# --- 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
The GUI has basically the same components as before, except that the axes are contained inside a uipanel
(similar to the screenshot of your GUI):
A few things to note:
Since our goal is to compare the figure's
CurrentPoint
to the axes position, it is important that they have the sameUnits
as that of the figure, thus:set(hAx, 'Units',get(hObject, 'Units'))
According to the documentation, an axis
Position
property is relative to its parent container, and because the four axes are inside a panel, we need to adjust their positions accordingly:axPos(idx,1:2) = axPos(idx,1:2) + ppos(:,1:2);
精彩评论