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 all variables except one or two

Subject: How to clear all variables except one or two

From: Diego Zegarra

Date: 31 Jan, 2009 23:40:17

Message: 1 of 9

I have a code that creates many variables and at each iteration I want to clear all the variables except for one or two. Is this possible without specifying clear A, clear B and so on for every variable?

I know clear all deletes them all but I need to keep one of them.

Thanks

Subject: How to clear all variables except one or two

From: Matt Fig

Date: 1 Feb, 2009 00:11:01

Message: 2 of 9

Do a search on the FEX for: keep





Ak6<k]eihkh*aca{dIppd#ldkak]lijq{{]u_kj]{{]{_^r]gp]U#e{{^qa

Subject: How to clear all variables except one or two

From: Diego Zegarra

Date: 1 Feb, 2009 00:37:01

Message: 3 of 9

Actually I found a way that can be done easily, this for anyone out there needing this info,

clearvars -except v1 v2 ... clears all variables except for those specified following the -except flag. Use the wildcard character '*' in a variable name to exclude variables that match a pattern from being cleared. clearvars -except X* clears all the variables in the current workspace, except for those that start with X, for instance.

Subject: How to clear all variables except one or two

From: Matt Fig

Date: 1 Feb, 2009 00:51:02

Message: 4 of 9

What version are you using? I don't have a clearvars command.




k{{_l{A{ueUpah6]ajhdjdqie{kk#^]qcga{p^#*{]{k]airIp]dkk_]<]l

Subject: How to clear all variables except one or two

From: Diego Zegarra

Date: 1 Feb, 2009 01:23:02

Message: 5 of 9

R2008a, I think it just came out starting this version.

Subject: How to clear all variables except one or two

From: Jan Simon

Date: 1 Feb, 2009 22:57:01

Message: 6 of 9

Dear Diego Zegarra!"

> I have a code that creates many variables and at each iteration I want to clear all the variables except for one or two. Is this possible without specifying clear A, clear B and so on for every variable?

Let the names of all variables, which should be cleared, start with a specific string, e.g.
"away_". Then "clear away_*" deletes them from memory.

By the way: clearing and recreation of variables is very time consuming. It could be more effective to use a cell instead of a cloud of variables.

Good luck, Jan Simon

Subject: How to clear all variables except one or two

From: Gireg

Date: 25 Jun, 2014 14:39:09

Message: 7 of 9

"Diego Zegarra" wrote in message <gm2nh1$4hu$1@fred.mathworks.com>...
> I have a code that creates many variables and at each iteration I want to clear all the variables except for one or two. Is this possible without specifying clear A, clear B and so on for every variable?
>
> I know clear all deletes them all but I need to keep one of them.
>
> Thanks

Hi Diego,
I was just looking for the same functionality today, that's why I answer you so late. You certainly have found a solution since. I have find a way that may be usefull to other Matlab users, so here is my solution:

clearvarlist = ['clearvarlist';setdiff(who,{'vartokeep1';'vartokeep2';'vartokeep3'})];
clear(clearvarlist{:});

Subject: How to clear all variables except one or two

From: dpb

Date: 25 Jun, 2014 15:03:03

Message: 8 of 9

On 6/25/2014 9:39 AM, Gireg wrote:
> "Diego Zegarra" wrote in message <gm2nh1$4hu$1@fred.mathworks.com>...
>> I have a code that creates many variables and at each iteration I want
>> to clear all the variables except for one or two. Is this possible
>> without specifying clear A, clear B and so on for every variable?
>>
>> I know clear all deletes them all but I need to keep one of them.
>>
>> Thanks
>
> Hi Diego,
> I was just looking for the same functionality today, that's why I answer
> you so late. You certainly have found a solution since. I have find a
> way that may be usefull to other Matlab users, so here is my solution:
>
> clearvarlist =
> ['clearvarlist';setdiff(who,{'vartokeep1';'vartokeep2';'vartokeep3'})];
> clear(clearvarlist{:});

There's also the -regexp pattern-matching alternative. Limited only by
your creativity in writing the regular expression.

--

Subject: How to clear all variables except one or two

From: Loren Shure

Date: 27 Jun, 2014 17:46:38

Message: 9 of 9


"Gireg " <laniole84@hotmail.fr> wrote in message
news:loemud$8p2$1@newscl01ah.mathworks.com...
> "Diego Zegarra" wrote in message <gm2nh1$4hu$1@fred.mathworks.com>...
>> I have a code that creates many variables and at each iteration I want to
>> clear all the variables except for one or two. Is this possible without
>> specifying clear A, clear B and so on for every variable?
>>
>> I know clear all deletes them all but I need to keep one of them.
>>
>> Thanks
>
> Hi Diego,
> I was just looking for the same functionality today, that's why I answer
> you so late. You certainly have found a solution since. I have find a way
> that may be usefull to other Matlab users, so here is my solution:
>
> clearvarlist =
> ['clearvarlist';setdiff(who,{'vartokeep1';'vartokeep2';'vartokeep3'})];
> clear(clearvarlist{:});

It's much simpler than that:

clearvars -except vartokeep1 vartokeep2 var2keep3


--
--Loren

http://blogs.mathworks.com/loren

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