clearvars does not work as indicated in documentation
    7 views (last 30 days)
  
       Show older comments
    
I'm having problems with clearvars.
Example 1:
>> A = 1; B = 2; C = 3; D = 4;
>> whos
  Name      Size            Bytes  Class     Attributes
    A         1x1                 8  double              
    B         1x1                 8  double              
    C         1x1                 8  double              
    D         1x1                 8  double              
>> clearvars -except C D
>> whos
>>
(in other words, all variables are cleared)
Example 2:
>> A = 1; B = 2; C = 3; D = 4;
>> whos
  Name      Size            Bytes  Class     Attributes
    A         1x1                 8  double              
    B         1x1                 8  double              
    C         1x1                 8  double              
    D         1x1                 8  double              
>> clearvars('-except','C')
>> whos
>> 
>> A = 1; B = 2; C = 3; D = 4;
>> whos
    Name      Size            Bytes  Class     Attributes
    A         1x1                 8  double              
    B         1x1                 8  double              
    C         1x1                 8  double              
    D         1x1                 8  double              
>> clearvars('*','-except','C')
>> whos
    Name      Size            Bytes  Class     Attributes
    A         1x1                 8  double              
    B         1x1                 8  double              
    C         1x1                 8  double              
    D         1x1                 8  double              
>>
(doesn't clear anything)
Any help?
2 Comments
  per isakson
      
      
 on 7 Sep 2015
				
      Edited: per isakson
      
      
 on 9 Sep 2015
  
			With R2013a
>> clear all
>> A = 1; B = 2; C = 3; D = 4;
>> clearvars -except C D
>> whos
  Name      Size            Bytes  Class     Attributes
  C         1x1                 8  double              
  D         1x1                 8  double
and
>> A = 1; B = 2; C = 3; D = 4;
>> 
>> clearvars('*','-except','C')
>> whos
  Name      Size            Bytes  Class     Attributes
  C         1x1                 8  double
Which version do you use?
  Guillaume
      
      
 on 8 Sep 2015
				clearvars works for me in 2015a. As per said, you need to tell us which version you're using.
Have you shadowed the built-in clearvars per chance? What does
which clearvars
return?
Answers (1)
  Jonathan Wharrier
 on 29 Nov 2022
        You might try 
    clearvars -except
this works with the latest release. I typed clearvars on its own and it did not but this seems to work and clears all.
0 Comments
See Also
Categories
				Find more on Workspace Variables and MAT Files in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

