Is there a way to automatically suppress Matlab from printing big matrices in command window?

只谈情不闲聊 提交于 2019-12-03 06:01:18
chappjc

One possibility is to overload the display function, which is called automatically when you enter an expression that is not terminated by ;. For example, if you put the following function in a folder called "@double" anywhere on your MATLAB path, the default display behavior will be overridden for double arrays (this is based on Mohsen Nosratinia's display.m for displaying matrix dimensions):

% @double/display.m
function display(v)
% DISPLAY Display a variable, limiting the number of elements shown.

name = inputname(1);    
if isempty(name)
    name = 'ans';
end

maxElementsShown = 500;
newlines = repmat('\n',1,~strcmp(get(0,'FormatSpacing'),'compact'));

if numel(v)>maxElementsShown,
    warning('display:varTooLong','Data not displayed because of length.');
    % OR show the first N=maxElementsShown elements
    % builtin('disp', v(1:maxElementsShown));
elseif numel(v)>0,
    fprintf([newlines '%s = \n' newlines], name);
    builtin('disp', v);
end

end

For example,

>> xx=1:10

xx = 

     1     2     3     4     5     6     7     8     9    10  

>> xx=1:1e4
Warning: Data not displayed because of length. 
> In double.display at 17 

EDIT: Updated to respect 'compact' and 'loose' output format preference.

EDIT 2: Prevent displaying an empty array. This makes whos and other commands avoid an unnecessary display.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!