Tanmay Bhatnagar
Tanmay Bhatnagar

Reputation: 2470

Showing command line output without all of the newlines

Let's just say I type 2+3 in MATLAB. It gives me this output :

>> 2+3

ans =

     5

Why is the output coming after 2 newlines? How do I correct this ?

Ideally, I would get the following output

ans = 5

Upvotes: 1

Views: 88

Answers (1)

Suever
Suever

Reputation: 65430

You can use the format command to change how the display of variables when printed. In your case, you'll likely want to use the 'compact' option

format compact

This will remove all of the unnecessary newlines.

2+3
% ans=
%    5

Unfortunately, there is no built-in way to display it all on the same line because MATLAB's display is built to deal with multi-dimensional data. You could overload the display command if you really wanted. You can create a folder named @double and then a function named display inside of that

@double/
    display.m

Then inside of display.m you could do something like this

function display(x)
    % If it's a scalar, then show it all on one line
    if isscalar(x)
        fprintf('%s = %g\n', inputname(1), x);
    else
        % Otherwise use the built-in display command
        builtin('display', x)
    end
end

Then it will automatically be used when you have a double variable

>> 2 + 3
% ans = 5

If you wanted to overload the display of other types of data (uint16, int8, uint8), you would need to do the same as above except put a copy within their @ folders as well.

Upvotes: 2

Related Questions