How to show a one column matrix with muiltcolumn

7 次查看(过去 30 天)
I have a matrix,such as
[1:17]'
the matlab will show it in a long shape,
I have to say it waste my space too much. So I hope to show it with 4 column. If I'm in Mathematica I just sove it like this
It's possible in matlab?

采纳的回答

Yode
Yode 2017-7-17
编辑:Yode 2017-7-17
As some improve,I complete this function almost perfect work for me
function multicolmn(mat,col)
if nargin<2
col=fix(floor(sqrt(numel(mat))));
end
if isnumeric(mat)
disp(sprintf([repmat('%d\t',1,col) '\n'], mat))
elseif iscell(mat)
n=max(cellfun(@length,mat))
disp(sprintf([repmat(sprintf('%%-%ds\t',n),1,col) '\n'],mat{:}))
end
It will show the input like a square shape as much as possible.Such as
>> multicolmn(1:17)
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17
Or
clear;
h=figure;
t=set(h,'pointer');
>> multicolmn(t)
arrow ibeam crosshair watch
topl topr botl botr
circle cross fleur custom
left top right bottom
hand
Of course,you can specify the colum you want to show
>> multicolmn(t,3)
arrow ibeam crosshair
watch topl topr
botl botr circle
cross fleur custom
left top right
bottom hand

更多回答(2 个)

Walter Roberson
Walter Roberson 2017-7-16
You would need to override the display() method for class "double", which would risk messing up the display of composite data types such as struct.
It would be safer to write a little routine that did this kind of display only when you asked for it.
One of the tricky parts is figuring out how wide the command window is, and figuring out the font size, so that you can figure out how many characters across you can display. The number of columns that fit will then depend upon the number of characters required to represent the largest number that happens to fit in that column -- for example you made your second column wider to fit the "10". But as you change the number of numeric columns then a wide number that was near the end of one column might move to a different column, which can change the number of characters required for the column, which can require changing the number of numeric columns again...It might be easiest to not try to narrow down the parameters by calculating from both sides, and to instead assume the maximum number of numeric columns, figure out whether the data would fit considering the widest in each, if not then reduce the number of numeric columns by 1, keep going until everything fits. (If you get down to 1 numeric column and it doesn't fit, then your command window is too small!)
  3 个评论
Walter Roberson
Walter Roberson 2017-7-16
Your example shows the user passing in the number of columns. Is that an acceptable interface, or does it need to figure out how many columns to use?
Yode
Yode 2017-7-16
编辑:Yode 2017-7-16
Yes,I need it to show 4 column.The number of column don't need to caculate

请先登录,再进行评论。


Yode
Yode 2017-7-16
编辑:Yode 2017-7-16
I get an answer in overflow
disp(sprintf([repmat('%d\t',1,4) '\n'], 1:17))
  1 个评论
Walter Roberson
Walter Roberson 2017-7-16
That is workable code, but I do note that it presents the output in a different order than you asked for, and it has the empty areas in a different location.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 数据的预处理 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!