Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to clear all variables except one or two

374 views
Skip to first unread message

Diego Zegarra

unread,
Jan 31, 2009, 6:40:17 PM1/31/09
to
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

Matt Fig

unread,
Jan 31, 2009, 7:11:01 PM1/31/09
to
Do a search on the FEX for: keep

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

Diego Zegarra

unread,
Jan 31, 2009, 7:37:01 PM1/31/09
to
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.

Matt Fig

unread,
Jan 31, 2009, 7:51:02 PM1/31/09
to
What version are you using? I don't have a clearvars command.


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

Diego Zegarra

unread,
Jan 31, 2009, 8:23:02 PM1/31/09
to
R2008a, I think it just came out starting this version.

Jan Simon

unread,
Feb 1, 2009, 5:57:01 PM2/1/09
to
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

Steven Lord

unread,
Feb 2, 2009, 10:49:53 AM2/2/09
to

"Diego Zegarra" <dieg...@gmail.com> wrote in message
news:gm2thm$mcb$1...@fred.mathworks.com...

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

You're correct, CLEARVARS was introduced in release R2008a.

http://www.mathworks.com/access/helpdesk/help/techdoc/rn/brjk1q5-1.html

--
Steve Lord
sl...@mathworks.com


Gireg

unread,
Jun 25, 2014, 10:39:09 AM6/25/14
to
"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{:});

dpb

unread,
Jun 25, 2014, 11:03:03 AM6/25/14
to
There's also the -regexp pattern-matching alternative. Limited only by
your creativity in writing the regular expression.

--

Loren Shure

unread,
Jun 27, 2014, 1:46:38 PM6/27/14
to

"Gireg " <lani...@hotmail.fr> wrote in message
news:loemud$8p2$1...@newscl01ah.mathworks.com...
It's much simpler than that:

clearvars -except vartokeep1 vartokeep2 var2keep3


--
--Loren

http://blogs.mathworks.com/loren

Blake

unread,
Jun 24, 2015, 10:02:10 AM6/24/15
to
"Diego Zegarra" wrote in message <gm2qrd$r1$1...@fred.mathworks.com>...
> Actually I found a way that can be done easily, this for anyone out there needing this info,
>
I DID need the info, and you have saved me a lot of time. Thanks!
0 new messages