有没有一种方法可以自动禁止Matlab在命令窗口中打印大型矩阵

有没有一种方法可以自动禁止Matlab在命令窗口中打印大型矩阵

本文介绍了有没有一种方法可以自动禁止Matlab在命令窗口中打印大型矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在matlab中是否有一个选项,一个插件/应用程序或一个窍门,使得如果您处于交互式命令会话中,则每次它打印出的矩阵对于人类来说都无法看得太大时,都会对输出进行编辑是警告矩阵有多大,还是矩阵的摘要(只有几行和几列)?

Is there an option in matlab or a plugin/app or a trick such that if you are in an interactive command session, every time it would print out a matrix way too big for a human to look through, it redacts the output to either a warning of how big the matrix is or a summary (only a few rows and columns) of the matrix?

很多时候我想在命令窗口中检查矩阵,但是我没有意识到矩阵有多大,所以我不小心将整个东西打印了出来.或某个函数中某个我没有编写自己代码的地方,有人错过了分号,然后递给它一个大矩阵,然后将整个内容转储到我的命令窗口中.

There are many times where I want to examine a matrix in the command window, but I didn't realize how big it was, so I accidentally printed the whole thing out. Or some place inside a function I did not code myself, someone missed a semicolon and I handed it a big matrix, and it dumps the whole thing in my command window.

有意义的是,在99.99%的时间内,人们不打算在其交互式命令窗口中打印一百万行矩阵,对吗?它会完全向其滚动缓冲区发送垃圾邮件,并删除您之前在屏幕上显示的所有有用信息.

It make sense that in 99.99% of the time, people do not intend to print a million row matrix in their interactive command window, right? It completely spams their scroll buffer and removes all useful information that you had on screen before.

因此,对于matlab而言,自动假定交互式会话中的用户想要输出一个大矩阵的摘要,而不是将整个内容都转储到命令窗口中,则更为有意义.设置中至少应该有这样的选项.

So it makes much more sense for matlab to automatically assume that the user in interactive sessions want to output a summary of a big matrix, instead of dumping the whole thing into the command window. There should at least be such an option in the settings.

推荐答案

一种可能是重载display函数,当您输入不以;终止的表达式时,该函数会自动调用.例如,如果将以下函数放在MATLAB路径上任何位置的"@double"文件夹中,则默认的display行为将被double数组覆盖(这基于Mohsen Nosratinia的display.m 用于显示矩阵尺寸):

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

例如,

>> 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

编辑:已更新,以遵守'compact''loose'输出格式首选项.

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

编辑2 :防止disp放置空数组.这使得whos和其他命令避免了不必要的display.

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

这篇关于有没有一种方法可以自动禁止Matlab在命令窗口中打印大型矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:08