How to suppress the "ans" line from MATLAB output?

EDIT . The above question is strictly related to the output that MATLAB creates by default in an interactive session , as illustrated by this example. I have no interest in changing the appearance of the output generated by scripts, functions, methods, etc.

In addition, the motivation for this is to keep most of my laptop extremely meager “screen real estate” for actual informative output.


Even with format compact MATLAB output includes the line ans = in addition to the line (s) that show the output properly. For instance.

 >> format compact >> date ans = 04-Sep-2012 >> 

Is there a way to suppress the string ans = so that, for example, the last interaction above looks like this:

 >> date 04-Sep-2012 >> 

... or at least like this:

 >> date ans = 04-Sep-2012 >> 
+4
source share
4 answers

This is a little complicated and can have other consequences, but if you mainly show data of a certain type (double, char, etc.), you can overwrite the corresponding built-in display method.

For instance,

 >> % Before overwriting the @char/display >> date ans = 04-Sep-2012 

Now create the @char directory at the location located on the MATLAB path and add a method called display.m :

 function display(x) disp(x) end 

Then you will have

 >> % After overwriting the @char/display >> date 04-Sep-2012 
+4
source

ans is just the name of the variable in which MATLAB saves its “last” response. The easiest way to “avoid this” is to simply assign the result to some other variable and print it clearly with fprintf or sprintf .

+3
source

disp(sprintf('<Your format>',<variables>) does the job.

+1
source

If you were creating a new class for your work, you would have some options. The "cute" display in the command window is created by the display method, which all classes inherit / Usually display prints the variable name, "= \ n", and then calls the disp method, which you are probably familiar with. ( help display for details).

However, for standard Matlab arrays, these methods are built-in, and I do not believe that they can be changed.

So, if you have endless options, if you create tools ( sprintf , fprintf , disp , various tools that crack the java base screen ), I don’t know how to set up a “more compact” default display style during quick interactive work.

0
source

Source: https://habr.com/ru/post/1432326/


All Articles