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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:49:35+00:00 2026-06-04T19:49:35+00:00

I have a MATLAB class that contains a method that employs a persistent variable.

  • 0

I have a MATLAB class that contains a method that employs a persistent variable. When certain conditions are met I need to clear the persistent variable without clearing the object to which the method belongs. I’ve been able to do this, but only by employing clear functions which has excessively broad scope for my purposes.

The classdef .m file for this problem:

classdef testMe

    properties
        keepMe
    end

    methods
        function obj = hasPersistent(obj)
            persistent foo

            if isempty(foo)
                foo = 1;
                disp(['set foo: ' num2str(foo)]);
                return
            end
            foo = foo + 1;
            disp(['increment foo: ' num2str(foo)]);

        end

        function obj = resetFoo(obj)
            %%%%%%%%%%%%%%%%%%%%%%%%%
            % this is unacceptably broad
            clear functions
            %%%%%%%%%%%%%%%%%%%%%%%%%
            obj = obj.hasPersistent;
        end
    end

end

A script that employs this class:

test = testMe();
test.keepMe = 'Don''t clear me bro';

test = test.hasPersistent;
test = test.hasPersistent;
test = test.hasPersistent;

%% Need to clear the persistent variable foo without clearing test.keepMe

test = test.resetFoo;

%%
test = test.hasPersistent;
test

The output from this is:

>> testFooClear
set foo: 1
increment foo: 2
increment foo: 3
increment foo: 4
set foo: 1

test = 

  testMe

  Properties:
    keepMe: 'Don't clear me bro'

  Methods

which is the desired output. The problem is that the line clear functions in the classdef file clears all functions in memory. I need a way to clear with a much smaller scope. For example, if hasPersistent' was a function instead of a method, the appropriately scoped clear statement would beclear hasPersistent`.

I know that clear obj.hasPersistent and clear testMe.hasPersistent both fail to clear the persistent variable. clear obj is similarly a bad idea.

  • 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-04T19:49:36+00:00Added an answer on June 4, 2026 at 7:49 pm

    Following the discussion in the comments, I think you want to use make foo a private property, accompanied with appropriate increment/reset public functions.

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

Sidebar

Related Questions

I have some MATLAB code and some Java code that need to talk with
I have a MATLAB class which contains a reference to a java object classdef
I have a Binary classification problem that I need to do in MATLAB. There
I have a Matlab program that generates a list x = 6.1692 8.1863 5.8092
I have a Matlab program that is running longer than I'd like it to.
I have a matlab script, lets call it master.m, that loads a file called
I have to write a Matlab script that does this: The input is 2
I have a matrix in MATLAB and I need to find the 99% value
I have several lists in Matlab that I want to write to the same
I thought that latest Matlab does this automatically. I have tested the following exe

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.