Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6204357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:04:37+00:00 2026-05-24T05:04:37+00:00

Edited Post These are the new functions that I have created using your template

  • 0

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

enter image description here
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.

enter image description here
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 if statements by mousing over the predetermined box. Then the GUI stops responding and will not open again until Matlab is restarted.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:04:37+00:00Added an answer on May 24, 2026 at 5:04 am

    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
    

    screenshot


    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):

    GUIDE

    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 same Units 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);

    GUI_in_action

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JavaScript resource that has the possibility of being edited at any
Problem: edited files on windows, using git-bash, to fix IE7 problems committed, pushed to
I have HTML code edited by FCKEditor stored in a database and would like
I'd like to write a plugin that does something with the currently edited file
(edited from original post to change BaseMessage to const BaseMessage&) Hello All, I'm very
I have written this code but it will print these stack traces in the
[EDIT] Alright, I edited this post since the code I posted back then had
I have ExtJS post data from a combofield to CodeIgniter. CodeIgniter reads the posted
i know there's a lot of these type of questions. i wanted to post
I'm running a post build vbs script on my installer project that edits one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.