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 9061677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:32:49+00:00 2026-06-16T15:32:49+00:00

In Matlab I have a class classdef myClass properties % some properties here… end

  • 0

In Matlab I have a class

classdef myClass
    properties
       % some properties here...
    end
    methods ( Access = 'public' )
        function obj = myClass()
            % constructor...
        end
        function obj = delete( obj )
             % suppose to be destructor...
             fprintf(1, 'delete\n');
        end
    end % public methods
end

What is the default behavior of Matlab when I clear a variable of type myClass?
For example:

>> m = myClass();
>> clear m

I would expect Matlab to call the destructor of m at this stage, but it seems like it doesn’t!!

My questions:

  1. How can I force a call to the destructor when clearing a variable?

  2. Is this default behavior of Matlab’s makes any sense? Isn’t it more logical to call the destructor when clearing a variable?

  3. Is it possible that Matlab’s classes do not have a detructor method (that is, there is no default method to be called when the class is destroyed)? Or am I missing something?

  4. Is it possible that only classes derived from handle have a destructor (the delete method)?

Thanks!

EDIT : following Jonas’ answer, a brief summary:

Matlab has two types of classes: value classes (the default) and handle classes (derived from handle super class). Value classes tends to provide better performance, however, they do not have a destructor functionality.

handle classes do have a destructor function: delete that is called when the class is destroyed. See this question from more details on handle class destructors.

If one wishes to have a destructor-like functionality for value classes, Jona’s answer proposes a method utilizing onCleanup functionality.

Thanks for the good answer and the insightful comments!

  • 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-16T15:32:51+00:00Added an answer on June 16, 2026 at 3:32 pm

    Delete is only defined as class destructor for handle classes, not value classes (so the answer to Q4 is “yes”, see the previous link to the documentation). Value classes work very much like standard Matlab arrays, in that they’re passed by value rather than by reference, and in that many of the internals, such as destructors, are hidden from the user. In exchange, they’re usually faster (see for example this SO question).

    Consequently, I suggest to use the onCleanup functionality if you want to have a delete method being called (note that delete(m) will not actually delete anything, so you may want to make that a private method).

    classdef myTestClass
        properties
           % some properties here...
        end
        properties (Hidden)
            cleanup
        end
        methods ( Access = 'public' )
            function obj = myTestClass()
                % constructor...
                obj.cleanup = onCleanup(@()delete(obj));
            end
        end 
        methods ( Access = 'private' )
            %# I suggest hiding the delete method, since it does not
            %# actually delete anything
            function obj = delete( obj )
                 fprintf(1, 'delete\n');
            end
        end % public methods
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a MATLAB object defined in a class file classdef foo properties
I have similar matlab and java classes: public class myClass { double[][] v1; String
For example, I have the following class: classdef testclass < handle properties buckets end
I define a MATLAB object class_save_test: classdef class_save_test methods function b=saveobj(a) fprintf('saveobj called.\n'); b=a;
I have class in matlab using classdef as, classdef scores % SCORES Summary of
I have a MATLAB class which contains a reference to a java object classdef
I have created a class-managed GUI in which the following methods are defined function
I have matlab file matlab_param.m function matlab_param(param1, param2) disp(sprintf('param1 : %s', param1)); disp(sprintf('param2 :
I have a Matlab function that creates a cell array with matrixes inside. It
I have installed Matlab r2010a on my computer I need use the function xlsread

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.