问题描述
在Matlab R2016b中,显示某些数据类型的变量会显示有关该类型的信息。当通过键入而没有最终分号来显示变量时会发生这种情况(使用 disp
函数时不会发生这种情况)。
In Matlab R2016b, displaying variables of certain data types shows information about the type. This happens when the variable is displayed by typing it without a final semicolon (it doesn't happen when using the disp
function).
比较例如:
-
Matlab R2015b(旧格式:仅显示数据):
Matlab R2015b (old format: displays just the data):
>> x = [10 20 30]
x =
10 20 30
>> x = {10 20 30}
x =
[10] [20] [30]
>> x = [false false true]
x =
0 0 1
Matlab R2016b(新格式:包括类型):
Matlab R2016b (new format: includes type):
>> x = [10 20 30]
x =
10 20 30
>> x = {10 20 30}
x =
1×3 cell array
[10] [20] [30]
>> x = [false false true]
x =
1×3 logical array
0 0 1
如你所见,R2016b中有一条额外的线告诉类型。显然,对于任何不是 double
或 char
的类型都会发生这种情况。
As you see, there's an extra line in R2016b telling the type. Apparently this happens for any type that is not double
or char
.
R2016b中是否有某些设置可以回到原来的行为?
Is there some setting in R2016b to go back to the old behaviour?
推荐答案
不幸的是似乎没有倾向于改变这种行为。 (一如既往)有一些hacky解决方法。
Unfortunately there doesn't seem to be a preference for changing that behavior. There is (as always) a bit of a hacky workaround.
当你从一行中省略一个分号时,它不是 disp
被调用,而是。 R2016b显然修改了 cell
数据类型的 display
方法,以显示一些类型信息以及值本身。
When you omit a semi-colon from a line, it's not disp
that is called but rather display
. R2016b has apparently modified the display
method for a cell
datatype to display some type information along with the values themselves.
值得庆幸的是,我们可以使用看起来更像显示的东西来重载
以前的版本。 display
方法
Thankfully we can overload that display
method with something that looks a little bit more like the display
of previous releases.
我们可以创建一个 @cell
文件夹(我们的任何地方)路径)并在里面放置一个名为 display.m
的文件。
We can create a @cell
folder (anywhere on our path) and place a file called display.m
inside.
@ cell / display.m
function display(obj)
% Overloaded display function for grumpy old men
if strcmpi(get(0, 'FormatSpacing'), 'loose')
fprintf('\n%s =\n\n', inputname(1))
else
fprintf('%s =\n', inputname(1))
end
disp(obj);
end
现在,每当显示一个单元格阵列时,由于缺少尾部分号,它不包含任何类型信息。
Now, whenever a cell array is displayed due lack of a trailing semi-colon, it will not include any type information.
>> c = {'a', 'b'}
c =
'a' 'b'
不幸的是,还有其他数据类型(例如 logical
)也会显示类型信息,因此您必须超载每个类的显示
方法。
Unfortunately, there are other datatypes (such as logical
) that also display the type information so you'd have to overload the display
method for each of these classes.
这篇关于回到Matlab R2016b中的旧显示格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!