Azzera filtri
Azzera filtri

Execute Command every time any command is executed

6 visualizzazioni (ultimi 30 giorni)
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 Commento
Adam
Adam il 18 Lug 2017
Modificato: Adam il 18 Lug 2017
You could create a wrapper function with tic toc into which you pass a function handle to the function you want to run.

Accedi per commentare.

Risposte (1)

Jan
Jan il 18 Lug 2017
Modificato: Jan il 18 Lug 2017
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 Commento
Tom DeLonge
Tom DeLonge il 18 Lug 2017
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...)

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Non è stata ancora inserito alcun tag.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by