round_circle
round_circle

Reputation: 995

Can I suppress command window output of a function in Matlab?

In Matlab I often use functions in my script that output something to the command window in one or several ways, for instance like this:

function output = mySubFunction(input)
    disp(['the input is: ', num2str(input)])
    output = input * 2;
    disp(['the output is: ', num2str(output)])
end

If I now use this function in my script it would look something like this:

data = 5;
disp('applying transformation...')
transformation = mySubFunction(data);
disp(['the result of the transformation is: ', num2str(transformation)])

This is of course a very simple example, in fact the functions are often quite complicated and give a lot of output. In this case, to keep a better overview of what is going on in my main script using my command window, I would like to suppress the disp() functions in my mySubFunction. Is there any way that I can do this simply from my main script (that is, without diving into possibly very complicated functions and subfunctions to comment out all disp() functions or insert semicolons)?

Upvotes: 2

Views: 1670

Answers (2)

Stewie Griffin
Stewie Griffin

Reputation: 14939

I believe you want evalc.

From the MATLAB-documentation (the function is similar in Octave):

Use the "evalc" function to redirect all the output displayed on the command window to a variable. This will not suppress figures, but it does allow you to prevent print statements from being displayed in the command window.

As an example:

[results, output] = evalc('mySubFunction(3)');

This displays nothing but stores the following variables in the workspace:

>> [results, output] = evalc('mySubFunction(3)');
>> ans
ans = 6
>> output
output = 6
>> results
results = the input is: 3
the output is: 6

Upvotes: 4

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22215

The typical way to do this in any language as far as I'm concerned, not just matlab / octave, is to have a 'debug' or 'verbose' variable present, which is checked against an if statement and decides whether to print those logs. The if statement could be used in the relevant code directly, or wrapped inside a 'prettier' function responsible for doing the logging.

This variable may be passed explicitly to the function, or it could be a global variable. If you plan to run your script directly from the shell rather than directly from a matlab/octave environment, it could even be detected from from a shell-defined environmental variable. Alternatively, it could be read from a config file. It all depends on your use case.


For the sake of an example, here is your session, transformed as above, using the global variable approach.

%% in file mainScript.m 
global VERBOSE
VERBOSE = true;
data = 5;
if VERBOSE, disp('applying transformation...'); end
transformation = mySubFunction(data);
if VERBOSE, disp(['the result of the transformation is: ', num2str(transformation)]); end

%% in file mySubFunction.m 
function output = mySubFunction(input)
    global VERBOSE
    if VERBOSE, disp(['the input is: ', num2str(input)]); end
    output = input * 2;
    if VERBOSE, disp(['the output is: ', num2str(output)]); end
end

Upvotes: 2

Related Questions