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

clearvars -except works as a command, but not function?

104 views
Skip to first unread message

Sean

unread,
Aug 21, 2015, 4:22:12 PM8/21/15
to
Could anyone help me understand why this code does not work:
clear
a=1;
b=3;
varnames = ['a b'];
clearvars('-except', varnames);
who

And yet this code does work:
clear
a=1;
b=3;
varnames = ['a b'];
eval(['clearvars -except ' varnames]);
who

The only difference is that in the former, I use function syntax, whereas in the latter, I use "command" syntax.

Sean

dpb

unread,
Aug 21, 2015, 5:04:44 PM8/21/15
to
And what's the error/makes you think it "doesn't work"? Works fine here...

--


Sean

unread,
Aug 21, 2015, 7:16:11 PM8/21/15
to
> > The only difference is that in the former, I use function syntax,
> > whereas in the latter, I use "command" syntax.
>
> And what's the error/makes you think it "doesn't work"? Works fine here...

Yep, sorry...
Execution of the "who" command for the first code snippet doesn't print anything to the Command Window. Sure enough, I can easily verify that after the first code snippet runs, the MATLAB workspace is empty (contains no variables).

Execution of the "who" command for the second code snippet results in the following being printed to the Command Window:
Your variables are:

a b

Sure enough, the variables "a" and "b" are there in the workspace.

When the first code snippet runs, it clears all variables. It doesn't honor the exception that I've called for. But the second code snippet honors the exception - it keeps the variables "a" and "b" in the workspace and clears everything else.

Doug Schwarz

unread,
Aug 22, 2015, 11:09:55 PM8/22/15
to
In article <mr819b$t3u$1...@newscl01ah.mathworks.com>,
"Sean" <sean.m...@gmail.com> wrote:

> Could anyone help me understand why this code does not work:
> clear
> a=1;
> b=3;
> varnames = ['a b'];
> clearvars('-except', varnames);
> who

You have tried to except a single variable named 'a b' and there is no
such variable.

Try instead

clearvars('-except','a','b')

If you have your variable names in a cell array,

varnames = {'a','b'};

then you can use

clearvars('-except',varnames{:})


>
> And yet this code does work:
> clear
> a=1;
> b=3;
> varnames = ['a b'];
> eval(['clearvars -except ' varnames]);
> who
>
> The only difference is that in the former, I use function syntax, whereas in
> the latter, I use "command" syntax.
>
> Sean

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Sean

unread,
Aug 23, 2015, 7:43:08 PM8/23/15
to
Doug Schwarz <s...@sig.for.address.edu> wrote in message <see-D96F82.2...@88-209-239-213.giganet.hu>...
> You have tried to except a single variable named 'a b' and there is no
> such variable.
>
> Try instead
>
> clearvars('-except','a','b')
>
> If you have your variable names in a cell array,
>
> varnames = {'a','b'};
>
> then you can use
>
> clearvars('-except',varnames{:})

Yep, sure enough, that works. Gahhh! Thanks Doug.

This is a frustrating one for me. Sure would be nice if MATLAB could throw a warning in such a scenario, to give me some breadcrumbs ("I see you're trying to do ___, but perhaps you meant to do ___ instead?"). I didn't realize that it would interepret my code as an attempt to except a single variable named 'a b'.

If nothing else, it would help if at least the clearvars doc page got an improvement, to cover the case where the list of variables to except is dynamic (in my case). But in general I still struggle from time to time with situations like this, where the "right" thing to do was make matlab spit out the contents of a cell array, e.g. varnames{:} in my case.

Steven Lord

unread,
Aug 24, 2015, 10:48:32 AM8/24/15
to


"Sean" <sean.m...@gmail.com> wrote in message
news:mrdlq7$q9h$1...@newscl01ah.mathworks.com...
> Doug Schwarz <s...@sig.for.address.edu> wrote in message
> <see-D96F82.2...@88-209-239-213.giganet.hu>...
>> You have tried to except a single variable named 'a b' and there is no
>> such variable.
>>
>> Try instead
>>
>> clearvars('-except','a','b')
>>
>> If you have your variable names in a cell array,
>>
>> varnames = {'a','b'};
>>
>> then you can use
>>
>> clearvars('-except',varnames{:})
>
> Yep, sure enough, that works. Gahhh! Thanks Doug.
> This is a frustrating one for me. Sure would be nice if MATLAB could throw
> a warning in such a scenario, to give me some breadcrumbs ("I see you're
> trying to do ___, but perhaps you meant to do ___ instead?"). I didn't
> realize that it would interepret my code as an attempt to except a single
> variable named 'a b'.

Your attempt to clear the two variables using a string 'a b' is the first
time I've seen someone try that approach. While we could include a warning
when we detect someone trying to do that, the check to see if we were in
that situation would take time and would slow down ALL uses of CLEARVARS,
not just the ones that fell into this category. Granted the slowdown would
be very small, I suspect, but given the relative frequencies of someone
calling CLEARVARS with 'a b' and someone calling CLEARVARS at all I'm not
sure issuing a warning makes sense. If you feel strongly about this, though,
please ask Technical Support to enter a request for a warning in this case
to the enhancement database.

> If nothing else, it would help if at least the clearvars doc page got an
> improvement, to cover the case where the list of variables to except is
> dynamic (in my case). But in general I still struggle from time to time
> with situations like this, where the "right" thing to do was make matlab
> spit out the contents of a cell array, e.g. varnames{:} in my case.

Something like the "Clear List of Variables" example on that documentation
page? In that case, the list of variables is dynamic (generated by the WHO
function.)

http://www.mathworks.com/help/matlab/ref/clearvars.html

If you don't think that's a sufficient or sufficiently clear example of your
use case, please scroll to the bottom of the screen and use the "No" button
to answer the question "Was this topic helpful?" You can then explain what
you feel was missing or what you'd like to see as feedback for the
documentation staff.

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Sean

unread,
Aug 24, 2015, 4:36:14 PM8/24/15
to
"Steven Lord" <Steve...@mathworks.com> wrote in message <mrfarp$kg9$1...@newscl01ah.mathworks.com>...
> Your attempt to clear the two variables using a string 'a b' is the first
> time I've seen someone try that approach. While we could include a warning
> when we detect someone trying to do that, the check to see if we were in
> that situation would take time and would slow down ALL uses of CLEARVARS,
> not just the ones that fell into this category. Granted the slowdown would
> be very small, I suspect, but given the relative frequencies of someone
> calling CLEARVARS with 'a b' and someone calling CLEARVARS at all I'm not
> sure issuing a warning makes sense. If you feel strongly about this, though,
> please ask Technical Support to enter a request for a warning in this case
> to the enhancement database.

Thanks for your response Steve. I don't feel that strongly about it, but I was envisioning more of a low-level detection. A call to CLEARVARS isn't the only situation in which one *should* use varnames{:}, but one might instead erroneously provide a string like 'a b'.

To your point about extra warnings causing slowdowns: I agree with your logic, but to float one idea which I'm sure MW has considered already...MW could provide a user-selectable mode (e.g. "Grandma mode"), which enables all sorts of extra detections, at the expense of speed. The default value could be "off." This is not all that different from when I run a Simulink model in debug mode with breakpoints, watch vars, etc, or from when I run Matlab with the Profiler on (in those cases, I am making the conscious choice to slow down my script / simulation, in return for more help from the debugger). Perhaps this mode already exists and I'm just unaware of it. If a user's script isn't working how they expected it to, they don't care about speed.

> Something like the "Clear List of Variables" example on that documentation
> page? In that case, the list of variables is dynamic (generated by the WHO
> function.)
>
> http://www.mathworks.com/help/matlab/ref/clearvars.html

Lol, yes...exactly like that example indeed. Sorry, I should have found that in the documentation. But thank you for your help.
0 new messages