I read in the documentation that I can use the statement echo on;
to have MATLAB print the statements it executes. However, I am having difficulties making this work inside a function.
For example:
function do_something(foo)
a = 2;
echo on;
foo = foo+1;
disp('This is a test');
foo = foo+3;
end
If I call do_something(foo)
from the command window, I was expecting to see something like:
foo = foo+1;
disp('This is a test');
This is a test
foo = foo+3;
However, in the example above, all MATLAB prints is:
this is a test
which is not what I was expecting.
As @Phonon explains below, echo on
is only for scripts (sorry I missed that!). However, I read in the documentation that I can activate echo
for a given function as follows:
echo fcnname on
So my question now is, is there a way to activate echo for a function using some variation of the syntax above so that I do this (call echo
) inside a function? (the variable that holds the function name in this case would be provided by mfilename
)
As far as I understand it is not possible to activate echo
for a particular function inside the actual function. Among other things, it seems that MATLAB needs to know beforehand if the function has to run in "echo mode" to avoid using JIT.
echo on turns on echoing for statements in all script files. When you turn on echoing, MATLAB® displays each line in the file in the Command Window as it runs. Normally, the statements in a script are not displayed during execution. Statement echoing is useful for debugging and for demonstrations.
Create a Script with Local FunctionsAdd all local functions at end of the file, after the script code. Include at least one line of script code before the local functions. Each local function must begin with its own function definition statement and end with the end keyword. The functions can appear in any order.
To display text in the Command Window, use disp or fprintf.
To run a live function in an installed version of MATLAB, call the function from the Command Window or from a script or live script.
According to Matlab documentation, i.e. help echo
,
ECHO ON
turns on echoing of commands inside Script-files.
It will not work inside functions. To make it work for functions, according to that same help file,
ECHO ON ALL
turns on the echoing of commands inside any Function-files that are currently in memory (i.e., the functions returned byINMEM
).
ECHO OFF ALL
turns them all off.
Update:
In order to find which function you're in currently, the best way I see of doing it is by calling dbstack
. Goes somewhat as follows:
[S,I] = dbstack;
funcName = S.name;
echo fcnname on
is the same as
echo('fcnname','on');
Using the function call syntax, you can pass a variable instead:
echo(found_found, 'on');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With