3

I feel a little troublesome to enter the clear command to clear the workspace, and then enter the clc command to clear the command window.

Is there a command or a way to clear the workspace and the command window at the same time?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Ray Chen
  • 182
  • 1
  • 2
  • 13
  • I feel like questions like this should be included as exercises in MathWorks documentation, training or Cody. It's a good example of thinking about how to do things within Matlab. Especially when it comes to customizing Matlab with `startup.m` and `finish.m`. – Matt Feb 28 '17 at 16:12
  • @Matt Yes, I agree. It will make the MATLAB documentation better. – Ray Chen Mar 01 '17 at 01:54

1 Answers1

2

There is not a built-in function to do both of these things at once.

Thankfully you can easily script this behavior by creating a script on your path that calls the commands that you want. For example, you could name your script clr.m and give it the following contents

clr.m

clear all;
clc;

If your intention is to completely clear everything, something like nuke (disclaimer: I am the developer) may be better suited to your needs as it is more complete.

Suever
  • 64,497
  • 14
  • 82
  • 101
  • Thanks for the quick and detailed answer. I think it is the best answer by far. Although I've just found creating a command shortcut can do this thing, creating a script should be quicker since I use keyboard more than using a mouse when testing and coding. ^_^ – Ray Chen Feb 28 '17 at 04:19
  • 2
    Just a note: `clear all` is usually overkill and unwarranted. People mostly want to just clear variables out of their RAM, which'd be `clear` (short for `clear vars`). `clear all` also removed pre-(temporarily)compiled functions, so calling those again would slow the computation, as it will be JITted again. – Adriaan Feb 28 '17 at 16:04
  • @Adriaan, Thanks for the follow-up. Now I got it and will use "clear". ^_^ – Ray Chen Mar 01 '17 at 01:37
  • 1
    And I found another way to clear the Command Window by using the command **home**, that "**clears**" the window and moves the cursor to the upper-left corner of the window, but does not empty the scroll buffer like the command **clc**. Thus, after using the command **home**, the "cleared" commands can still be searched out in the **Find** dialog of the Command Window. See [here](https://www.mathworks.com/help/matlab/matlab_env/find-text-in-command-window-or-history.html) for details. – Ray Chen Mar 01 '17 at 01:46