Matlab clc command - matlab

I was checking the original 'clc.m' file in MATLAB. Apparently the function is written as a p-code and you just see the description which is placed in:
..\MATLAB\R2013a\toolbox\matlab\iofun\clc.m
How can I take a look at the original code?; however this is not the main question, it is just for fun.
The main point is I am looking for a way to reverse the clc process, after you clean the screen. Is there any way to reverse the clc process. The same question goes for clear all as well.

Try using home instead of clc. Whereas clc removes all text from the Command Window and moves the cursor to the top left giving you a blank window, home just moves the cursor to the top left and gives a you a blank window - but the text is still there, and you can scroll up to see it. I use home all the time rather than clc.
In either case, the text remains in the Command History window, and can be retrieved in the Command Window using the Up/Down arrows.
The reason you can't see the code behind clc is not that it's p-coded, it's that it's a built-in function (i.e. not implemented in the MATLAB language). The same is true for clear, and also many math functions such as svd, eig etc. There's no way to modify them to change what they do (such as reversing the process).
Edit: You might also like to look into the diary function, which keeps a log of all the input and output at the command window in a specified file. I have the following lines in my startup.m file (type doc startup if you don't know how to use MATLAB startup files):
diaryFolder = 'C:\diaries';
diaryFileName = ['diary', datestr(now, 'yyyymmdd'), '.txt'];
diary(fullfile(diaryFolder, diaryFileName))
So whenever I start MATLAB, it's automatically capturing all command window input and output to a diary file that's named by the date - If I start MATLAB multiple times a day, it just appends to the same file. I can clc or home whenever convenient, and there's always a record kept of everything that I can search through if necessary.

I don't believe there would be anyway to undo the clc process. That said, it is possible to still view the command history, so that might get you somewhere.
Once you clear the data, I believe it's gone forever. I highly doubt that data is stored in memory somewhere. I know that after a certain number of lines, the history is deleted. So your best bet is to start figuring out how large that buffer is and proceed from there to see if you can find anything interesting.

Related

waitbar matlab before start a standalone script

I did a standalone application in Matlab and it works. The only problem is that when I launch the application, it takes time before start asking to the user some file (it is the first think the program has to do). The user does not understand if the program is working or not, since no message neither symbol of working progress appear on the screen.
My idea is to show a waitbar until the window asking the file to user appears.
How can I do this? is it possible to use the waitbar outside a loop?
The script starts as follow:
close all
clear all
[filename,pathname] = uigetfile({'*.xlsx'},'Opening File','C:\');
I don't know why, it takes time before open the window for choosing the file.
The time between launch and file selection input appearing is most likely due to the time it takes to load the MCR. You could add a splash screen to your compilation.
If the end user is running from a command line wrap your exe in a system/shell which writes to the command window that the application is starting.
Your issue is most likely the use of clear all. This makes MATLAB remove all variables (in scope, global and persistent), and compiled scripts from memory, forcing it to recompile and load everything again.
If your purpose is to clear all variables in the current scope, you should be able to increase the initial speed of your script by only running clear instead.
Even faster speed can be achieved if you specify which variables to clear using clear var1 var2 ...

Alternatives to `clear(function_name)` to remove function from RAM?

As stated in MATLAB's FAQ,
1.3.3.1. Why, when I edit a function file in MATLAB, is the change not seen by MATLAB until everything is cleared or MATLAB is restarted?
When you write an M-file in MATLAB, you can either write a script or a
function. The difference is that a script is read from the disk and
parsed line by line each time it is called. A function is loaded into
RAM for execution. Because it is loaded into RAM, when you edit a
function, that change is not loaded into RAM until a call to the new
function is made.
To get MATLAB to recognize your edited function, type
clear functions to clear all functions, or
clear <function name> to clear just your function out of RAM.
This is a major pain when I'm developing a function & editing it repeatedly (I use an external editor most of the time). I was thinking of putting in a final line, at least during debug, like
clear(myfunc)
but I'm concerned about unwanted side effects. Does anyone know if there are any?
Further, I'd rather have a way to configure MATLAB so it doesn't automatically store called functions in RAM once the top-level function (i.e. the one called from the console) terminates. Is that even possible?
EDIT: I should mention that MATLAB's behavior is inconsistent. Sometimes my edits take effect once I save the m-file, other times they don't even if I'm editing with the MATLAB IDE editor window.
In all honesty MATLAB has a very powerful editor so you really should
be using it. It will make you life easier. (just an opinion)
Unless you are running the code by stepping through it no change to code will be run until the code is re-run (preferably with a cleared workspace).
clear(myfunc) will clear the variables created by that function in upto that point. You can add at the end of any function or script clear variableA variableB for all variables you want cleared at the end. This will give you control to only clear what you want. Once variables are cleared the only effect will be that if those variables are called again later in the code an error will occur since they no longer exist.
If your simply testing that particular function and want to save time by not calling inputs each time and want to clear the workspace and command window at the same time. You can add the following code just beneath the function definition.
If you leave the following code and call the function from another function or script, the code will be ignored as long as the inputs are available to it externally. Anything you add to the if statement will only occur when you call the function with no inputs doc nargin. You could add it to the top level function and you would simply press the run button or f5 without having to type in the command window to run the code.
function [ output1, output2 ] = blah( input1, input2 )
if nargin == 0
clc
clear all
%above two lines will clear the workspace and command window when you run
%the function
%define function inputs
%(optionally add the following to behave as if you inserted a breakpoint
%in the location just before the error occurred (great for debugging)
db stop if error
end
*you can also add a short-cut to snippets of code up in the top right of the MATLAB interface to clear the workspace etc when they are clicked.
Hackish* idea for this:
I'd rather have a way to configure MATLAB so it doesn't automatically store called functions in RAM once the top-level function (i.e. the one called from the console) terminates. Is that even possible?
* This is the sort of thing I might use, while advising others on why it is a bad idea, also probably uses undocumented (thus unreliable) things.
Specifically if your using an external editor I guess your going to have to click on the command window to run the function...
commandWindowHandle = ...
handle(com.mathworks.mde.desk.MLDesktop.getInstance.getClient('Command Window'...
.getComponent(0).getComponent(0).getComponent(0),'CallbackProperties');
commandWindowHandle.MouseClickedCallback = 'clear functions'
It doesn't clear the function definitions on exiting a function rather on clicking on the command window
note: the callback will not fire if a function is currently running
For a potentially more reliable but more wasteful version:
commandWindowHandle.KeyPressedCallback= 'clear functions'
will help* ensure definitions are clear, but is wasteful as every keystroke will run 'clear functions'... although after one keystroke this will be quicker as there will be no functions in memory!
* No guarantee I know there are ways in which this could fail... having at least 1 keystroke in the command window after the previous function call has finished would be advisable!
To disable these set the callback to empty string ''
commandWindowHandle.MouseClickedCallback = ''
commandWindowHandle.KeyPressedCallback= ''

How to stop a Matlab script but don’t kill the process? [duplicate]

This question already has answers here:
Stop and continue execution from debugger possible?
(6 answers)
Closed 8 years ago.
Strg+C stops and kills a Matlab script (at least sometimes). But is there a way to stop a Matlab, take a look at some variables and continue the calculation?
I am not talking about just setting a breakpoint. I want my script, let’s say run for couple hours come back to it hit some buttons that stops the calculations take a look at some variable and then continue the calculation.
I tried to find out if there is some shortcut key for this – I am quite sure there isn’t.
Now I was thinking about including an if-case that looks if a certain button was pressed by the user. If so there would be a useless k=0 line and a breakpoint on it. And if no one is pressing this button the loop would continue. But this is where my limited Matlab knowledge leaves me. I don’t know if there is a way to ask for a user-button press but don’t wait for a button press like in the function input. Also I just have a running script, I don’t have any GUI.
To drop to the command prompt you need the command keyboard and then type return when you have finished (you don't need a breakpoint). The tricky bit is how to trigger it. There a few options. The easiest is to open a figure window. The following code halts the process when any key is pressed.
keyDownListener=#(src,event) keyboard;
fig = figure;
drawnow
set(fig,'KeyPressFcn',keyDownListener)
for p=1:10000
%do some thing
end
You can modify this to test for a specific key since the keypress is contained within the event struct.
To use no figure gui at all its more of a problem. I'm not aware of a non blocking keyboard input method. A mex file the runs kbhit() in C might do it, but kbhit() is not standard C so it would only work on Windows. An easier option maybe to test for the presence of a file.
for p=1:100000
if exist(fullfile(pwd,'halt.tmp'),'file')
keyboard
end
%do something here
end
This drops to the debug console when halt.tmp is created in the current directory.
Other potential methods could involve using multiple threads to read 'input' (either the Parallel computer toolbox or undocumented Java code), or using http://psychtoolbox.org/ as mentioned by #bdecaf

Disable auto-scrolling in command window

A lot of the code that I write in Matlab has a very verbose output. As the program runs, information is printed to the command window, and with each new line, the window automatically scrolls to the bottom. This becomes a problem when I want to read some of the output more closely or scroll up to look at older output. I can scroll up, but only until a new line is printed, which is often less than a second.
Does anyone know if it is possible to turn off this automatic scrolling in the Matlab window? I work in a number of different Matlab versions, depending on the machine, and this happens with all of them. The answer to this might be "No", but I swear I remember having this functionality at one point.
Use the more function: http://www.mathworks.com/help/matlab/ref/more.html
more on
Then run your program. Press spacebar when you wish to see more of the output.
more off will turn it off.
You may find this workaround useful.
First launch matlab using the command line matlab -logfile 'myLog.txt' (the doc says it "starts MATLAB and makes a copy of any output to the Command Window in filename. This includes all crash reports.")
Then open your .txt file using a text editor supporting automatic refresh of content (see picture). On OSX I use TextWrangler (freely available at www) but others have been reported to have this feature (see here or here).
Results: output displays (fprintf, disp, but not the commands per se) are printed both on the Matlab console and the text editor (file is refreshed with a little lag time, below half a second I would say with my configuration). And there is no automatic scrolling. Such procedure does not seem to impact the overall performance of the script (although it may deserve some testing).

What is the usual way in MATLAB to read help page by page?

I'm looking for an equivalent of for example DOS's dir |more which lists the data, until one page is complete, then waits for a key to be pressed until showing another. Is there an equivalent for MATLAB's help system.
I know I could simply scroll back, but this would be so much more convenient, expecially if one uses help system often.
Type more on at the command line. This will print command outputs page by page.
For the next line: 'return'; next page; 'spacebar'; return to command line: 'q'.
Likewise, more off resumes the normal display mode.