Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB "echo on" inside a function. Is it possible?

Tags:

matlab

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.

Update

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)

Update 2:

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.

like image 925
Amelio Vazquez-Reina Avatar asked Nov 04 '11 14:11

Amelio Vazquez-Reina


People also ask

What does echo on do in MATLAB?

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.

How do you write a function inside a script?

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.

How do you print output in MATLAB?

To display text in the Command Window, use disp or fprintf.

Can you call a function in the Command Window MATLAB?

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.


2 Answers

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 by INMEM).

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;
like image 178
Phonon Avatar answered Oct 13 '22 09:10

Phonon


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');
like image 21
Ben Voigt Avatar answered Oct 13 '22 09:10

Ben Voigt