2

I don't know why Matlab doesn't do this, but if no output arguments are assigned for a function, I would prefer my functions not to output to the console.

E.g.

function out=getmagic(n)
    out=magic(n);
    figure; 
    plot(out(1,:));
end

For high numbers this is increasingly more annoying if you forget the ; mark at the end of the line when calling the function. My solution so far is to include an ifstatement at the end of the function:

if nargin==0 %no output argument is requested
    out=[]; %shorten ouput argument to prevent flooding of console
end

is there any better way of doing this (e.g. wouldn't give any output at all)?

user2305193
  • 2,079
  • 18
  • 39
  • 2
    To prevent flooding the console, put a `;` in the end of your function call... If you forgot to put the `;` that is a coding error. MATLAB doesn't fix your code according to your intentions. Just go and fix your code. Also, you meant `nargout`? – Ander Biguri Feb 05 '18 at 16:35
  • 1
    It's a design choice by MathWorks I would like to change and others may also dislike. The question is how to adjust this design, not whether it complies with existing design choices, otherwisely stated: the answer I'm looking for is not _just comply with existing design choices_. – user2305193 Feb 05 '18 at 16:39
  • 1
    Its part of the language.... If you don't like it, use python, or C. It is possible that there is a solution, but that would just be encouraging bad MATLAB code design – Ander Biguri Feb 05 '18 at 16:42
  • 2
    I get that part. But there are ways around it in this case, and people who know how. It seems legit to ask for knowledge about that here.. – user2305193 Feb 05 '18 at 16:44
  • That can also be a good question when you need to release a function that is idiot-proof. I sometimes have the impression that guys here are mostly sole researchers or at least doesn't develop tools for users, so they tend to stick the "right principle theorm" but with no regard to front-end considerations – Adiel Feb 05 '18 at 18:27
  • @Adiel sorry, I don't understand, can you elaborate a bit more in laymans terms? – user2305193 Feb 05 '18 at 18:33
  • 2
    Your question can be one that a developer ask himself when he build a function that designed to be used by others, so you can't promise that they will use `;` when they run it, but you also don't want to spam their command window. It's not a good example because external users usually doesn't work from command window, but there is some ignorance here from that aspect of programming ("acrobatic" code vs readable one, for example). Researchers widely use matlab, and they usually write code for themselves only. – Adiel Feb 05 '18 at 19:09

1 Answers1

3

I would typically do something like this:

function varargout = getmagic(n)

    out=magic(n);
    figure; 
    plot(out(1,:));

    if nargout>0
        varargout{1} = out;
    end

end

Now if you call it with an output argument you'll get the value of out; but if you call it with no output argument, you'll get nothing at all.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64