Yes, this week's picks are these 3 lines of code:
clear all close all clc
Is this a joke?! No, not really.
In my day-to-day job as an application engineer, I show how MATLAB can be used to solve people's problems. When I'm in front of customers, I do demo after demo, switching context left and right. I need a quick way to reset everything so that I can show the next thing they need to see. I need to run the three commands above frequently during any single customer engagement.
ccc and clr both accomplish this with a short, single command. It seems very trivial, but it's probably one of the most used commands in my job. I also use the toolstrip and shortcuts often, so I have a shortcut for ccc.
The code behind it is
Note that I actually use "close all force" to close any hidden figures.
By the way, there's an interesting discussion about the use of "clear all" in the comments section. Personally, I think it's okay as long as you know what it does, and in my case it makes sense for my day-to-day work.
Tell us about your simple shortcut commands you created to make your life just a little easier. Also, if you do have a command or a shortcut for clearing everything, what do you call it? My colleagues Sean calls it cll and Brett calls his purge. Feel free to leave a comment for Chad or Brenden as well.
Get the MATLAB code
Published with MATLAB® R2013a
14 CommentsOldest to Newest
Is it possible to save breakpoints, clear, and restore breakpoints?
Great question! That’s one of the things that were discussed in the comments section for “ccc”.
Instead of “clear all“, you can just use “clear” or “clear variables“, as described in the documentation. This will leave the breakpoints untouched.
It is really useful. I use Matlab in classrooms a lot and do the same thing, except I call the button shortcut “BLEACH”
Seems like you were short on ideas the past week.
Really useful. Wish to see ‘ccc’ as a default option in the next MATLAB release.
@Shalin, what you are looking for is a tool that I developed called MATLAB State found here: http://www.mathworks.com/matlabcentral/fileexchange/37486-matlab-state.
I too have had a short-cut for years for exactly this need. I called it Clean Start (with an empty desktop icon) and it contains the following code:
delete(timerfindall); % remove any persisting timer objects and their userdata
I added the timerfindall because I discovered “clear classes” (or “clear all”) is not strong enough to delete these objects. I followed the discussion about this here and on File Exchange with interest, so I have expanded my clear start into a standalone script, adding Stefan’s (http://www.mathworks.com/matlabcentral/fileexchange/authors/287715) useful technique for persisting break-points. Will probably upload to File Exchange in due course.
Yes, it is trivial, I use this loads along with my close-window short-cuts, see http://www.mathworks.co.uk/matlabcentral/fileexchange/39152-apply-standard+-keyboard-shortcuts-keys-to-a-figure-window
Another thing that I like is for this to work in debug mode and take care of the stack and base workspace:
I find programs like this to be particularly dangerous. You say “I need a quick way to reset everything”, but the result of ccc and clr are a long way from resetting everything (e.g., timers and instruments don’t get deleted and the random number generators don’t get reset). The selections have no error checking and essentially no documentation. I cannot imagine trying to really document exactly what the programs do, which in general is a sign of a bad program.I much prefer to define my ccc shortcut as
which takes a little longer, but puts me in a known state.
That made me smile. That’s certainly a perfect way to really reset everything.
I do agree that “ccc” or “clr” will not reset everything (if you take the word “everything” literally). At least for me, it has served its purpose, and I have never had an issue with it.
The main point I wanted to make with this post was that, if people are typing the same commands over and over again, like “clear all; close all; clc”, then they should think of creating a shortcut. Yes, it should probably have documentation, but I was focusing more on the convenience of it, and not on the programming technique used.
You certainly have a point. I’ll also restart MATLAB the second there is any indication that something has changed one of my default HG properties.
Honestly, whenever I am using something like the clear command I did a little. I can see the use when doing teaching in MATLAB, but otherwise I feel that the need to use clear all is just a sign off bad programming habits. Especially scripts that have it as the starting point.
As for the shortcut functionality: I feel it has lost some of its strength in the latest MATLAB versions because you have to switch tabs now before getting to them.
Just so that you know, you can also make use of the Quick Access Toolbar for those shortcuts that you need all the time. In the screenshot below, the bar that’s just below the toolstrip is the Quick Access Toolbar (which by the way can be moved from the top right of the screen). I have my “ccc” there as well so that I can access it anytime, anywhere.
I have a similar shortcut but also add the command:
for cases where there is an error reading a file and it may not have been properly closed.