Clear Filters
Clear Filters

How to show a one column matrix with muiltcolumn

3 views (last 30 days)
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?

Accepted Answer

Yode
Yode on 17 Jul 2017
Edited: Yode on 17 Jul 2017
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

More Answers (2)

Walter Roberson
Walter Roberson on 16 Jul 2017
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 Comments
Walter Roberson
Walter Roberson on 16 Jul 2017
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 on 16 Jul 2017
Edited: Yode on 16 Jul 2017
Yes,I need it to show 4 column.The number of column don't need to caculate

Sign in to comment.


Yode
Yode on 16 Jul 2017
Edited: Yode on 16 Jul 2017
I get an answer in overflow
disp(sprintf([repmat('%d\t',1,4) '\n'], 1:17))
  1 Comment
Walter Roberson
Walter Roberson on 16 Jul 2017
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.

Sign in to comment.

Categories

Find more on Particle & Nuclear Physics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!