MATLAB Examples

Clearing

Ideally we would not need to rely on the MATLAB clear command. All of our variables would be scoped to the correct workspace. However particularly in interactive use or dealing with other people's code, we need to issue the clear command

Contents

A counter

I have implemented a MATLAB function that increments the output value by one each time. How did I do it?

myCounter
myCounter
myCounter
ans =

     1


ans =

     2


ans =

     3

Resetting the counter

The counter uses a persistent variable. How can we reset the counter back to zero? myCounter does not provide a documented approach to do this.

clear myCounter
myCounter
myCounter
ans =

     1


ans =

     2

The nuke commands

The "nuke" commands, clear all or the slightly more direct clear functions have the following side effects:

  • Reduce performance of the MATLAB execution engine
  • Clearing MEX files
  • Everything is cleared

Nuke protection

See mlock and munlock

Alternatives

How might we implement myCounter to be more friendly to reset