Displaying Progress Status of Long Running Script, Part 4: Display-Erase Utility
Sometimes when displaying the progress of a long running script, there is so much output at the command window that there is too much to scroll through or the buffer fills up and you can’t go back to see full the history.
In this code-along style video I write a utility, based on my colleague Sean’s suggestion that displays new status information after erasing the previous information. This allows progress to displayed without filling up the command window.
Features used in this video include:
- Displaying characters and strings
- Storing state with nested functions
Play the video in full screen mode for a better viewing experience.
Function is here:
function f=makeEraseDisplayFunction
% Length of last string
len=0;
% Return handle to function
f = @displayFunction;
% Nested Function
function displayFunction(str,varargin)
if nargin>=2
eraseThisTime=varargin{1}; % True or false
if ~eraseThisTime
len = 0;
end
end
% Erase previous characters
eraseStr=repmat('\b',[1 len] );
fprintf(eraseStr);
% Display New characters
fprintf(str);
% Decide what to do at next call
if nargin == 3
eraseNextTime=varargin{2}; % True or false
if eraseNextTime
len = length(str);
end
len = 0;
else
len = length(str);
end
end
end
评论
要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。