Execute Command every time any command is executed

6 次查看(过去 30 天)
This might be a weird question. I am looking for some way to always execute a command/script BEFORE any script is being executed.
More specifically, I want to know the time when the root of the calling stack has been called. I know, that in principle I can do this with 'tic' and 'toc'. But I do not want to place tic and toc at the beginning of every function/script/command prompt I call... Is there a way of doing this? E.g. event trigger?
  1 个评论
Adam
Adam 2017-7-18
编辑:Adam 2017-7-18
You could create a wrapper function with tic toc into which you pass a function handle to the function you want to run.

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2017-7-18
编辑:Jan 2017-7-18
Running a "script BEFORE any script is being executed" is a contradiction and the direct implementation would cause an infinite recursion.
The wrapper mentioned by Adam can look like this:
function varargout = myStart(fcn, varargin)
tic
[varargout{1:nargout}] = fcn(varargin{:});
end
Now start you function not as
yourFcn(1, 2, 'hello')
but as
myStart(@yourFcn, 1, 2, 'hello')
You could attach a listener to the status message of the CommandWindow, which displays 'busy' during computations. Perhaps some details from FEX: CmdWinTool might be useful. But I would not create such indirect triggers. It is meta-programming to parse the screen output of Matlab to detect a certain event. Better insert the tic exactly where you need it and not by a magic tricks.
  1 个评论
Tom DeLonge
Tom DeLonge 2017-7-18
Thanks Jan Simon for the suggestion. I would calling my function just the same way as always. No matter from where I call it (e.g. inside another function/script/...)
Perhaps I'll elaborate a bit more what I am doing. I found Loren's post of 2007 on how to monitor progress using text output very appealing. You can find it here: Monitoring Progress of a Calculation. What she does is removing text by printing a backspace character like so:
fprintf(1,'\b',);
I found this very pleasant and replaced all my function output with a function called writeConsoleCont which does just that: It replaces previously written output. I even made it a bit more fancy. writeConsoleCont does not just delete the whole previous console output but only replaces output that came from the same line of code.
writeConsoleCont does that by looking into the function call stack and check which function and which line of code it has been called from. It stores all textual output in a global cell array. On each call of writeConsoleCont, it deletes all output and rewrites it with the modified cell array.
However, this global cell array should be flushed once the code execution has finished or is interrupted by CTRL + C. Alternatively, the global cell could be flushed on the first call of writeConsoleCont. And this is why I wanted to know when code execution as some sort of "unique ID"...
until now I just flush the cell array manually, but this has become a big pain...
Okay, this is super confusing, I uploaded the function to the file exchange: writeConsoleCont at FileExchange
(I added plotting capabilities as well, don't get confused by this part...)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

尚未输入任何标签。

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by