18

Let's say you are some new programmer and you do something like...

%...la da da
%...programming away
if such && such
    clear = 1;
else 
    clear = 0;
end 

or in some other way, you assign the variable clear a value.

Is there some way to "clear" clear?

clearvars doesn't work. Clicking the workspace variable and manually clicking delete does work, but I think it's cheating.

braX
  • 11,506
  • 5
  • 20
  • 33
Frederick
  • 1,271
  • 1
  • 10
  • 29
  • OK thanks guys for making my code completely matlab-syntax compatible. haha. – Frederick Aug 21 '13 at 14:55
  • 2
    You can also just delete the variable in the Matlab UI by clicking on it a pressing delete... – Dan Aug 21 '13 at 15:28
  • 2
    This is still interesting if you are in nodesktop mode. – Dennis Jaheruddin Aug 21 '13 at 15:50
  • 2
    related article by [Loren Shure](http://stackoverflow.com/users/113700/loren): [A Clear Conundrum](http://blogs.mathworks.com/loren/2011/01/27/a-clear-conundrum/) – Eitan T Aug 21 '13 at 22:50
  • @EitanT I really like the way I wrote the question. – Frederick Oct 07 '13 at 15:05
  • 1
    @Frederick Okay, but why are you rolling back the edits blindly? For instance, I believe that the word "MATLAB" in the title in supposed to be written in capitals. I've also added a link to `clearvars` and a new tag to the question. And what new information does the code snippet add? – Eitan T Oct 07 '13 at 15:09

4 Answers4

27

This will do it:

builtin('clear','clear')

Note: Keep in mind to avoid such operations to keep code clarity. Only do overwrite when it is the exact action you want to take place. Otherwise it may cause future bugs if you forgot (or if another person uses your code and didn't realize it) that you have the clear (or any other) function overwritten. You could easily name this variable as doClear for example.

Werner
  • 2,537
  • 1
  • 26
  • 38
Buck Thorn
  • 5,024
  • 2
  • 17
  • 27
8

Any name, even builtin and feval can be overriden. In such case, you can use function handles instead to force MALTAB into interpreting a statement as a function call:

clear = str2func('clear');
clear('clear')

Obviously, str2func can also be overrriden! :) however, there exists a similar solution (inspired by Loren's article), which is creating a separate m-file that does the same thing:

function clearclear()
    assignin('caller', 'clear', @clear);

Calling this function in the main workspace should allow you to do clear('clear') safely.

The second solution takes advantage of the fact that the m-file doesn't "see" the variable clear in the main workspace, and therefore can access the actual handle of the clear function properly.

Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • I thought this was a more elegant solution, but it doesn't work. (at least in R2012a). Error: "clear" was previously used as a variable, conflicting with its use here as the name of a function or command. – Frederick Aug 21 '13 at 18:48
  • Actually you cannot assign a variable to builtin. You can overload it with a function but then you really break everything (even the cross to close matab!). --- Sidenote, it seems that `feval('clear')` is the simplest solution mentioned in the comments to the blog. – Dennis Jaheruddin Aug 22 '13 at 07:52
  • 1
    @DennisJaheruddin The `feval` method is prone to the same problem. By the way, I've just created two variables named `feval` and `builtin`. Overloading `builtin` does seem to break stuff, but it _is_ possible. – Eitan T Aug 22 '13 at 07:55
  • Interesting, on 2012b simply doing `builtin=1` in a clear workspace gives you an `Index exceeds matrix dimensions` and when you type `who` it indeed shows that builtin is a variable. However, nothing shows up in the workspace. – Dennis Jaheruddin Aug 22 '13 at 07:58
1

A non intuitive way is

clear = rand(1000,500,700);
pack

This produces the following warning:

Warning: Variable 'clear' cannot be saved to a MAT-file whose version is older than 7.3. To save this variable, use the -v7.3 switch. Skipping...

It also suffers from the same issue that you can assign pack to be a variable.

StrongBad
  • 869
  • 6
  • 16
0

Interesting problem! I found it surprisingly hard to find an ways to do this programatically (besides the one suggested by @TryHard)

Here is the I have come up with though it is a bit more powerfull than clear:

!matlab &
exit

Note that if you want to type this in the command line at once, you need to use a shift+enter in between.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
  • 2
    I don't think restarting matlab should count either! (What if I gave `exit` a value too though...?) – Frederick Aug 21 '13 at 15:38
  • @Frederick then try `quit`. Regardless, with this you should be able to continue your work in the new session after having programatically solved the `clear` issue. – Dennis Jaheruddin Aug 21 '13 at 15:49
  • What if a second session is already running? [It has been mentioned](http://stackoverflow.com/questions/18204663/run-a-script-that-uses-multiple-matlab-sessions#comment26681566_18204664) that MATLAB allows you to open at most two simultaneous sessions under the same license... – Eitan T Aug 21 '13 at 22:59
  • 1
    @EitanT Upon reading the two comments below that I think it is fine. As long as you keep it on 1 computer and all for yourself. – Dennis Jaheruddin Aug 22 '13 at 07:39