"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