Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to clear persistent without clearing breakpoints

Subject: How to clear persistent without clearing breakpoints

From: Eddie

Date: 19 Oct, 2010 15:10:05

Message: 1 of 15

Hi

I've tried "clear all" and "clear function", but that also clears my breakpoints. Is there another way to clear persistent variables without clearing breakpoints in my function?

Cheers

Subject: How to clear persistent without clearing breakpoints

From: Sean

Date: 19 Oct, 2010 16:19:03

Message: 2 of 15

"Eddie " <epatton@yahoo.com> wrote in message <i9kccc$7u7$1@fred.mathworks.com>...
> Hi
>
> I've tried "clear all" and "clear function", but that also clears my breakpoints. Is there another way to clear persistent variables without clearing breakpoints in my function?
>
> Cheers

how about:
clear persistent
:)

Subject: How to clear persistent without clearing breakpoints

From: ImageAnalyst

Date: 19 Oct, 2010 16:47:17

Message: 3 of 15

On Oct 19, 12:19 pm, "Sean "
<sean.dewol...@nospamplease.umit.maine.edu> wrote:
> "Eddie " <epat...@yahoo.com> wrote in message <i9kccc$7u...@fred.mathworks.com>...
> > Hi
>
> > I've tried "clear all" and "clear function", but that also clears my breakpoints. Is there another way to clear persistent variables without clearing breakpoints in my function?
>
> > Cheers
>
> how about:
> clear persistent
> :)

-----------------------------------
I think you have to clear them individually, one at a time, by name:
clear('myPersistentVariableName');

Subject: How to clear persistent without clearing breakpoints

From: Eddie

Date: 21 Oct, 2010 17:36:04

Message: 4 of 15

Hi

Thanks for your suggestions. I tried

clear persistent
clear('my_persistent_var')

and neither work. My persistent variable is still defined within the function. It seems the only way to get rid of them and keep the breakpoint is to edit the file.

I wonder if there is a "touch" command where I can touch the file thus causing Matlab to purge the persistent variables in that function.

Cheers

Subject: How to clear persistent without clearing breakpoints

From: Sean

Date: 21 Oct, 2010 17:46:04

Message: 5 of 15

"Eddie " <epatton@yahoo.com> wrote in message <i9ptm4$flk$1@fred.mathworks.com>...
> Hi
>
> Thanks for your suggestions. I tried
>
> clear persistent
> clear('my_persistent_var')
>
> and neither work. My persistent variable is still defined within the function. It seems the only way to get rid of them and keep the breakpoint is to edit the file.
>
> I wonder if there is a "touch" command where I can touch the file thus causing Matlab to purge the persistent variables in that function.
>
> Cheers

Hmm. You could put an additional logical input to your function and then if it's true clear the variable in your function:

function out = example(in1,in2,purge);
persistent x
if purge; clear x; end
%or: if purge; x = 0; end

Subject: How to clear persistent without clearing breakpoints

From: ImageAnalyst

Date: 21 Oct, 2010 18:49:04

Message: 6 of 15

On Oct 21, 1:46 pm, "Sean "
<sean.dewol...@nospamplease.umit.maine.edu> wrote:
> function out = example(in1,in2,purge);
> persistent x
> if purge; clear x; end
> %or: if purge; x = 0; end
------------------------------------------------------------
Sean, I think he'd need to do your second option of resetting to some
known default value, because he said he tried to clear the persistent
variable by name and that didn't work. But I don't know why he'd want
to clear it anyway since he'll eventually need to use it again. The
sensible thing to do seems to be to just have some flag (like "purge")
where you can assign it to some known, starting, default value.

Subject: How to clear persistent without clearing breakpoints

From: Sean

Date: 21 Oct, 2010 19:01:04

Message: 7 of 15

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <8a7504e5-58d3-4b35-ab14-549ee8b108e8@l20g2000yqm.googlegroups.com>...
> On Oct 21, 1:46 pm, "Sean "
> <sean.dewol...@nospamplease.umit.maine.edu> wrote:
> > function out = example(in1,in2,purge);
> > persistent x
> > if purge; clear x; end
> > %or: if purge; x = 0; end
> ------------------------------------------------------------

> Sean, I think he'd need to do your second option of resetting to some
> known default value, because he said he tried to clear the persistent
> variable by name and that didn't work. But I don't know why he'd want
> to clear it anyway since he'll eventually need to use it again. The
> sensible thing to do seems to be to just have some flag (like "purge")
> where you can assign it to some known, starting, default value.

IA,
I assumed he tried clearing it from the command window, not from inside the function. I made a sample function below where I tried clearing x, I then called it a few times. It works, if you "purge" this function the next iteration releases x as 1. If you don't it's incremented on every call.

function out = trypersistent(purge)
persistent x
if isempty(x)
    x = 1;
else
    x = x+1;
end
out = x;
if purge; clear x; end

Subject: How to clear persistent without clearing breakpoints

From: Jan Simon

Date: 21 Oct, 2010 23:27:03

Message: 8 of 15

Dear Eddie,

> I've tried "clear all" and "clear function", but that also clears my breakpoints. Is there another way to clear persistent variables without clearing breakpoints in my function?

You can clear both persistent variables and break points, but restore the breakpoints afterwards:
  saved = dbstatus
  clear YourFunctionName
  restoreDBStatus(saved)

And a function like this:
  function restoreDBStatus(S)
  for iS = 1:length(S)
    aS = S(iS);
    Name = aS.Name;
    if length(Name)
      subInd = strfind(Name, ' (');
      if length(subInd)
        Name = Name(1:subInd(1) - 1);
      end
      for iL = 1:length(aS.Line)
        dbstop(Name, sprintf('%d', aS.line(iL)));
      end
    end
  end
  
Good luck, Jan

Subject: How to clear persistent without clearing breakpoints

From: Eddie

Date: 25 Oct, 2010 17:32:04

Message: 9 of 15

Hi All

Thanks for ideas! Its great to know how to reload the breakpoints.

Too bad Matlab doesn't have a script for this.

Cheers

Subject: How to clear persistent without clearing breakpoints

From: Steven_Lord

Date: 25 Oct, 2010 17:45:05

Message: 10 of 15



"Eddie " <epatton@yahoo.com> wrote in message
news:ia4euk$ekp$1@fred.mathworks.com...
> Hi All
>
> Thanks for ideas! Its great to know how to reload the breakpoints.
>
> Too bad Matlab doesn't have a script for this.

You mean like DBSTATUS in conjunction with DBSTOP?

http://www.mathworks.com/help/techdoc/ref/dbstatus.html

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How to clear persistent without clearing breakpoints

From: Jan Simon

Date: 25 Oct, 2010 18:35:04

Message: 11 of 15

Dear Steven,

> You mean like DBSTATUS in conjunction with DBSTOP?

Exactly! Thanks to mention this "new" feature:
  saved = dbstatus;
  clear functions
  dbstop(saved)

I tried to find out, when this feature of DBSTOP was added, looking in the "old docs":
  http://www.mathworks.com/help/doc-archives.html
But infortunately I cannot enter these pages with my standard account, because it is not associated to a specific license. Is this really useful -- for the users or for TMW?
What about students working with a group license? It is not a hard work for me to decide which license I should associate to my account. But I will not take the time to associate my students manually.

Kind regards, Jan

Subject: How to clear persistent without clearing breakpoints

From: Steven_Lord

Date: 26 Oct, 2010 01:02:01

Message: 12 of 15



"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message
news:ia4iko$c0h$1@fred.mathworks.com...
> Dear Steven,
>
>> You mean like DBSTATUS in conjunction with DBSTOP?
>
> Exactly! Thanks to mention this "new" feature:
> saved = dbstatus;
> clear functions
> dbstop(saved)
>
> I tried to find out, when this feature of DBSTOP was added, looking in the
> "old docs":
> http://www.mathworks.com/help/doc-archives.html
> But infortunately I cannot enter these pages with my standard account,
> because it is not associated to a specific license. Is this really
> useful -- for the users or for TMW?
> What about students working with a group license? It is not a hard work
> for me to decide which license I should associate to my account. But I
> will not take the time to associate my students manually.
>
> Kind regards, Jan

I don't have any control over that; I recommend that you send this feedback
to Customer Service, who can pass it along to the appropriate members of our
staff.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How to clear persistent without clearing breakpoints

From: Eddie

Date: 29 Oct, 2010 00:40:04

Message: 13 of 15

Hi Steve

What would be REALLY REALLY nice is a clear command that could clear persistents in all functions. It doesn't make sense why the persistent is only cleared when the function is edited or you issue "clear function": the latter also wiping out the breakpoints.

If I use the above scripts to recover breakpoints, I also have to keep track of the function name. Kinda klunky.

Thanks for your help.

Cheers

Subject: How to clear persistent without clearing breakpoints

From: Steven_Lord

Date: 29 Oct, 2010 02:54:35

Message: 14 of 15



"Eddie " <epatton@yahoo.com> wrote in message
news:iad554$1s6$1@fred.mathworks.com...
> Hi Steve
>
> What would be REALLY REALLY nice is a clear command that could clear
> persistents in all functions. It doesn't make sense why the persistent is
> only cleared when the function is edited or you issue "clear function":
> the latter also wiping out the breakpoints.

I recommend you send this enhancement request to Technical Support along
with a brief description of how you'd use this, so that they can enter it
into the enhancement database (including the information about your use
case.)

> If I use the above scripts to recover breakpoints, I also have to keep
> track of the function name. Kinda klunky.

Why do you need to keep track of the function name? Does this do what you
want?

function clearPersistent(filename)
oldBreakpoints = dbstatus;
clear(filename)
dbstop(oldBreakpoints)

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How to clear persistent without clearing breakpoints

From: Jason

Date: 22 Oct, 2013 15:11:16

Message: 15 of 15

"Steven_Lord" <slord@mathworks.com> wrote in message <iadd1b$nrj$1@fred.mathworks.com>...
>
>
> "Eddie " <epatton@yahoo.com> wrote in message
> news:iad554$1s6$1@fred.mathworks.com...
> > Hi Steve
> >
> > What would be REALLY REALLY nice is a clear command that could clear
> > persistents in all functions. It doesn't make sense why the persistent is
> > only cleared when the function is edited or you issue "clear function":
> > the latter also wiping out the breakpoints.
>
> I recommend you send this enhancement request to Technical Support along
> with a brief description of how you'd use this, so that they can enter it
> into the enhancement database (including the information about your use
> case.)
>
> > If I use the above scripts to recover breakpoints, I also have to keep
> > track of the function name. Kinda klunky.
>
> Why do you need to keep track of the function name? Does this do what you
> want?
>
> function clearPersistent(filename)
> oldBreakpoints = dbstatus;
> clear(filename)
> dbstop(oldBreakpoints)
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


The above solutions do not appear to work for me (I'm using R2013b). My immediate use is the m file that accompanies a GUI developed in GUIDE. I would like to clear all persisent variables when the GUI is opened, but also need to set breakpoints for troubleshooting. The m file is fairly self contained - it has all the functions the GUI needs. In the first function of the m file, the one that launches the GUI, the very first three lines I put:

    saved = dbstatus;
    clear functions
    dbstop(saved)

however, when I start the GUI it still still clears all breakpoints. I've also tried saving the contents of the "saved" variable to a mat file and then reloading it after the clear command, but that makes no difference.

We also have a UAV simulator that makes extensive use of persistent variables and the cleanest way to reset them at the beginning of the simulation is to use "clear functions", but again doing so makes debugging a pain because all the breakpoints get cleared.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us