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

  • SEARCH
  • Home
  • 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 8020839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:48:17+00:00 2026-06-04T21:48:17+00:00

I have created a class-managed GUI in which the following methods are defined function

  • 0

I have created a class-managed GUI in which the following methods are defined

function h = make_figure(this)
    h = figure('CloseRequestFcn',@this.close_figure);
    this.openFigures(end+1) = h;
end

function close_figure(this, src, event)
    this.openFigures(this.openFigures == src) = [];
    delete(src);
end

Throughout the life of the GUI maney figures are created and closed with the aid of the methods above. Now the weird part…

If I double click a figure file eg (myfigure.fig) in MATLAB’s “Current Folder” window and then try to close it (while the GUI is still alive) then the close_figure method of the GUI’s class runs although it wasn’t created with h = figure('CloseRequestFcn',@this.close_figure); of make_figure method!!! This is the first weird part. The second weird part is that, although an unintented part of code is executed, it shouldn’t create problems because the command this.openFigures(this.openFigures == src) = []; actually leaves this.openFigures unchanged and practically all that close_figure does is to delete(src) which is desirable. Despite the above, my program behaves… weird after the above happens.

I have noticed that if I hit figure in the command window and then close it, everything is OK, the close_figure method is not executed. The problem reveals itself only when opening a .fig file created by the GUI’s class.

Any ideas?

  • 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-06-04T21:48:18+00:00Added an answer on June 4, 2026 at 9:48 pm

    I just tried the scenario you described, and I am not seeing any weird behavior; create instance of the class, use it to make a figure, then open a FIG file from the “Current Folder” file browser, and close it..

    Here is the class definition I used:

    classdef MyFigureClass < handle
        properties
            openFigures = [];
        end
    
        methods
            function h = make_figure(obj)
                h = figure('CloseRequestFcn',@(src,evt) close_figure(obj,src,evt));
                obj.openFigures(end+1) = h;
            end
    
            function close_figure(obj, src, evt)
                obj.openFigures(obj.openFigures == src) = [];
                delete(src);
            end
    
        end
    end
    

    (Tested in R2012a on Windows XP 32-bit.)


    EDIT

    Ok I think I know what the problem is. Here are the step to reproduce. First add the following method to the class above:

    function save_figure(obj, idx)
        saveas(obj.openFigures(idx), sprintf('fig%02d.fig',idx))
    end
    

    Now using our class wrapper, lets create a figure, and save it to FIG file:

    f = MyFigureClass();
    f.make_figure()
    f.save_figure(1)       %# creates fig01.fig
    

    Next clear your workspace clear all, and move the class definition file MyFigureClass.m to some other folder (something not on the path). Now try to load the saved figure hgload fig01.fig, and it will throw the error:

    Warning: Cannot load an object of class 'MyFigureClass':
    Its class cannot be found. 
    > In C:\Program Files\MATLAB\R2012a\toolbox\matlab\graphics\hgload.p>localLoadFile at 167
      In C:\Program Files\MATLAB\R2012a\toolbox\matlab\graphics\hgload.p>hgload at 59
      In openfig at 72
      In open at 159
      In uiopen at 196 
    

    You will have to issue the following command to forcefully close it:

    close all force
    

    EDIT2

    One solution (as mentioned in the comments), is to reset the figure close callback function just before saving it. Here is how I modified the save_figure function above:

    function save_figure(obj, idx)
        %# backup callback function
        fcn = get(obj.openFigures(idx),'CloseRequestFcn');
    
        %# set default callback function
        set(obj.openFigures(idx),'CloseRequestFcn','closereq')
    
        %# save figure
        saveas(obj.openFigures(idx), sprintf('fig%02d.fig',idx))
    
        %# restore callback function
        set(obj.openFigures(idx),'CloseRequestFcn',fcn)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've defined Class A which has number of methods. Then I have this other
I have created a class library called AddServiceLibrary in which I have a method
I am using C#. I have created a class which can be included in
I am using the apache library. I have created a class which sends a
I have an Assembly(A) which defines a Managed class which has a public constructor
I have created a class that will manage connection and commands, I named it
I have created a class named Times and I have to construct 4 overloaded
I have created a Class Library (Core Processing Component) using C# in Visual Studio
I have created a class as follows: #include <iostream> #define DLLEXPORT _declspec( dllexport )
I have created a class library as a project. Where I have added a

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.