You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
clear all output code
211 views (last 30 days)
Show older comments
Is it possible in matlab live script to "clear all output" with a program code?
12 Comments
KALYAN ACHARJYA
on 12 Aug 2019
Can you elaborate?
ga rec
on 14 Aug 2019
In the code is a loop and the live script should only present the output in the script after the second run.
Therefore I only want to erase the output in the script with a code; like it is possible with right-click anywhere in the script and select Clear All Output
Rik
on 16 Aug 2019
Despite the efforts from Mathworks to push people to use live scripts, I expect most power users will still use normal scripts and functions.
I would suggest you prevent your code from returning any output when you don't want it to, instead of resorting to clearing it. Something like the rudimentary example below.
A=zeros(1,4);
for n=1:2
A=A+rand(1,4);
if n==2
disp(A)
end
end
ga rec
on 16 Aug 2019
Thanks for the answer. I think I have to precisise my issue:
I have a lot of calculations and I want to display some of them at determined positions in the output where the required results are not calculated yet.
I think the easiest way with the least effort is to calculate the results in the first run of a loop and display them at the second run of the program. Therefore I need a solution to "Clear All Output" in the code. I think I need a handle for the "Clear all Output"-button, but I can't find one.
Tom Pickering
on 3 Nov 2020
Hi, could you please give some more detials on the desired workflow? I am trying to understand when and why you want outputs to show, and when and why you want (some or all) outputs to clear in a Live Script.
Walter Roberson
on 3 Nov 2020
Recently someone asked about clearing livescript output. What they were looking for was a way to put some output (progress information) at the top of the screen, which can be done with the regular command window by clc;fprintf.
Neither the regular command window nor livescript permit a program to output to a particular screen location.
The usual workaround for progress information in the command window is to Carriage Return and write enough new output to erase what was previously on the line.
(I am not sure why waitbar is not used.)
Adam Danz
on 3 Nov 2020
In the Live Editor environment we have the option [Clear all Output] under the View tab. Many people, including myself, want to execute this programmatically. So, any workflow that includes that button is a workflow that could be done programmatically, if we had such a command.
For example, many people use a script to clear everything (example), to set Matlab to a clean state that removes all figures, clears all variables, and clears the command window. But when you're working with a Live Script, you have to take the extra measure of selecting the view tab > pressing the clear-all-output button.
Another example is when adding code to a live script after an embedded figure is produced, the new code goes under the figure until the live script regenerates the content. That's annoying but so is clicking on a tab and then a button to clear the content. Instead, I'd like to add the clear-live-script command to my existing shortcut that cleans up the matlab console.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/402204/image.png)
I don't think an existing commands should clear the live script. Instead, I think there should be an additional command or flag to clear live scripts. That way live script content is only cleared intentionally. For example,
clear livescript
% or
cls
The functionality already exists and is invoked when the clear-all-output button is pressed. The new function or flag would do the same thing.
K.
on 10 Nov 2020
Regarding Walter's comment, I think I found the post where someone was trying to use clc and fprintf to show textual progress updates. If anyone is interested, I have posted a different workaround for textual progress indication that works in the Live Editor. Essentially, you can use the '\b' (backspace) character to remove characters from textual output in the Live Editor.
Walter Roberson
on 10 Nov 2020
Yes, that is the one. Thanks, K.
Answers (2)
Heather Lai
on 18 Oct 2020
I would also like to encourage this functionallity. When I write code which I would like to operate iteratively after a user input, it would be very helpful for the output to be programatically cleared.
1 Comment
Chris Kloppers
on 22 Oct 2020
I also need to have this functionality. Mainly for good housekeeping, but I can think of many reasons why I would need it. It will even help to clear the output only for the current section.
Sai Bhargav Avula
on 20 Aug 2019
Currently you cannot programmatically set the clear output in Live Editor. There are only two ways to do this. First, the one you mentioned and the other by right clicking anywhere in the script and selecting the Clear All Output option.
19 Comments
Sai Bhargav Avula
on 20 Aug 2019
Edited: Sai Bhargav Avula
on 20 Aug 2019
To the best of my knowledge it is not possible to create a handle for the Clear All Output. A possible work around, if there are no figures to be displayed would be to use the "evalc" function to redirect all the output displayed to a variable. The same way you redirect the outputs that are to be displayed on the command window.
ga rec
on 20 Aug 2019
Thanks for the answers.
@ Sai Bhargav Avula: I have a lot of figures in the output
@ Ric: I have the same idea Rik, but therefore i need a lot of additional code in the livescript environment and for presentation reasons it's not a solution for me with all the additional code.
Does nobody has an idea how to create a handle for the button?
Guillaume
on 20 Aug 2019
Typically, matlab does not give you programmatic access to its interface, so the answer to "how to create a handle for the button" is very likely: it's not possible.
There are some undocumented ways to hack into some of the interface (mostly the editor and command window) but as far as I know, there's no way to interact with the toolbar.
Adam Danz
on 5 Jun 2020
I also want a programmatic way to "Clear all outputs" in the live editor. I don't see why it would be so difficult to create a function similar to clc, clear, close for the live editor. Obviously the method already exists and is called when the "Clear all outputs" button is pressed so why can't there be a function that just calls upon that method? To me, this feature is an obvious one that's missing.
Michelle Hirsch
on 9 Oct 2020
Adam, could you say more about what you'd like to do with a programmatic clear all outputs?
Adam Danz
on 9 Oct 2020
Edited: Adam Danz
on 11 Oct 2020
I'd like to easily clear the embedded figures created in live script editor, programmatically. I realize there's a button that can be pressed but I would just like a function that evokes the same process. For example, I have a small script clr that looks something like this,
clear
close all
clc
and when I run that, I'd like the live script editor to be cleared of any embedded figures.
Sindar
on 16 Feb 2021
Here's my use case for a clear-livescript function: loop-plot-save
I have livescripts where the purpose is to:
- call other functions to create figures
- add to or combine these figures
- save new figures
Sometimes I need to play with a single figure, so livescript functionality is useful. Often, though, I've figured it out and want to loop over several figures. At the end of each loop, I'd like to clear output for a few reasons:
- livescripts occasionally do things I don't want, such as plotting in subplots of figures I didn't think had focus
- my figures often take up a lot of memory that I want cleared once they've been saved
- the livescript gets cluttered by the many figures and text output
Similarly, I sometimes need to loop over complicated plotting functions, but don't actually need the created plots. In some cases, I could easily adjust the functions themselves (e.g., add a "silent" option), but this isn't always feasible or worth my time
goc3
on 19 May 2021
I second Adam's comments here. I also have a custom script to run clear, close all, and clc, which I include in many .m files. Doing such things in .mlx files is not straightforward.
In particular, though, I am also needing a solution to the posted question/request for slightly a different reason. When using a live script to export a report (using Report Generator) that includes figures, it is preferable to have the figures closed/removed from the live script after being run. The figures end up in the generated report, where they are desired, but I'd rather keep the live script clear of all figures for easier readability and smaller file size.
Nathaniel H Werner
on 17 Jun 2022
Agreed, this should be addressed. It is very off putting.
Christopher Wong
on 10 Dec 2022
Actually, I think the Clear All Ouput option is fine. IMO the Live Editor is not for hardcore automation—it's more of a supplementary GUI. I don't mind right-clicking as needed.
Marco Riani
on 1 Feb 2023
This is just to say that a lot of times I would have liked to clear the embedded figures created in live script editor, programmatically. It is mysterious the reason why you still cannot do it programmatically.
Jeremy Riousset
on 24 Mar 2023
I just wanted to vote that one up. I have the same request for the same reasons.
Gokhan Pekcan
on 23 Mar 2024
Obviously this is a much desired capability (which must already exists in another form); it's been requested for over three years... please...
Jack
on 9 May 2024
I would also like this capability. Right clicking and picking Clear All Output is not a good solution. A code sequence similar to "clc" but for the live editor output window would be great. You could then assign it to the quick access toolbar for fast execution.
Thanks
Duncan Carlsmith
on 23 May 2024
Me too!
See Also
Categories
Find more on Adding custom doc in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)