5

I have a short function which uses textscan to read data into a variable.

My problem is that I always get this:

>>function('function.txt')

    ans = 

        {10x1 cell}    {10x1 cell}    {10x1 cell}    [10x1 double]

Is there any way to suppress this, apart from adding a semi colon to the end of the line I use to call the function? I'd like to be able to suppress it without adding the semi colon. I don't want to display anything at all when running this function, I just want to load my file.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Mark Hughes
  • 245
  • 2
  • 5
  • 11
  • Oops, mis-read the question. Am deleting my answer. – Colin T Bowers Jan 07 '13 at 05:17
  • 4
    Have re-read the question properly now :-) What you want can be done using `evalc` (I think). See [suppress-output](http://stackoverflow.com/questions/9518146/suppress-output) and [suppressing-a-functions-command-window-output](http://stackoverflow.com/questions/3029636/suppressing-a-functions-command-window-output) for near duplicates of this question (I'm flagging this as a possible duplicate). – Colin T Bowers Jan 07 '13 at 05:21

3 Answers3

5

You can suppress the output by remove output arguments (or return values) of the function. OR Try use Variable Number of Outputs, see Support Variable Number of Outputs

function varargout = foo
    nOutputs = nargout;
    varargout = cell(1,nOutputs);
    for k = 1:nOutputs;
        varargout{k} = k;
    end
end

You type >>foo and get nothing. You type >>a=foo and get >>a=1. You type >>[a,b]=foo and get >>a=1 >>b=2.

You can thus suppress output by NOT providing any output arguments.

Richard Dong
  • 704
  • 1
  • 7
  • 13
4

The simplest way to avoid having output printed out is to not assign the first output argument if no output argument was requested:

function [aOut,b,c] = doSomething

%# create a,b,c normally
a = 1;
b = 4;
c = 3;

%# only assign aOut if any output is requested
if nargout > 0
   aOut = a;
end
Jonas
  • 74,690
  • 10
  • 137
  • 177
1

You could try using the diary functionality. It redirects all input and output from the command prompt to a file of your choice. If you only turn it on during a specific function, no input should be captured. I admit it is a bit of a clumsy solution as the diary on/off state is global to matlab, but it might be ok in your case.

Read more about it here: Diary matlab help

Adriaan
  • 17,741
  • 7
  • 42
  • 75
KlausCPH
  • 1,816
  • 11
  • 14