How do I align decimal points in a table?
显示 更早的评论
Here is a sample of my data. I would like to align the decimal points to make it easy to read. How do I do this?
Entropy Min Max std
_______ _________ _______ ________
xyz_z 6.2908 0.0060019 0.83143 0.15786
xyz_y 6.2812 0.011033 0.81358 0.17433
xyz_x 6.1703 0.016483 0.76458 0.16028
RGB_B 5.6583 0 0.89412 0.13687
RGB_G 5.6379 0.062745 0.91373 0.15714
RGB_R 5.5792 0 0.94118 0.17313
Gray_WA 5.5706 0.086455 0.91075 0.15458
NTSC_Y 5.5706 0.086455 0.91075 0.15458
Gray_A 5.5398 0.11111 0.8902 0.14837
HSV_V 5.5371 0.21961 0.94118 0.11085
YCbCr_Y 5.3517 0.13699 0.84492 0.13275
HSV_S 5.0647 0 1 0.17222
HSV_H 4.6601 0 0.99858 0.10478
NTSC_I 3.5758 -0.2332 0.37468 0.053372
YCbCr_Cr 3.2791 0.32766 0.74433 0.034347
YCbCr_Cb 3.2367 0.1561 0.7043 0.033095
NTSC_Q 0.55185 -0.20632 0.12336 0.023349
回答(4 个)
Les Beckham
2023-2-13
编辑:Voss
2023-2-14
I think I found a solution. Use the Figure Space
s = compose('%10.4f', 50*rand(5,1));
snew = cellfun(@(c) strrep(c, ' ', char(0x2007)), s, 'UniformOutput', false)
fig = uifigure;
uit = uitable(fig, 'Data', snew)
Result

7 个评论
Matt O'Brien
2023-2-13
super.... have shut down for the evening.... will follow up tomorrow evening.
Thanks.
Les Beckham
2023-2-13
You are quite welcome.
If this turns out to work for you tomorow, please Accept the answer. Thanks.
Matt O'Brien
2023-2-13
移动:Voss
2023-2-13
I was able to do a quick test. I replaced the space with a non breaking space (ie Char(160)).
This gives a similar result to your example. In my case, the decimal points do not align, but the column seems to be centre aligned. I would prefer the underscore method even though it is not very elegant.

I will need to work out what the following line of code actually does..... and try that tomorrow night.
snew cellfun(@(c) strrep(c, ' ', char(0x2007)), s, 'UniformOutput', false)
Thanks again
Matt O'Brien
2023-2-13
移动:Voss
2023-2-13
I was able to digest part of your suggested line of code.
In my code I replaced spaces with char(0x2007)... and got the following...

This meets my needs... I will flag your response as the answer.
Walter Roberson
2023-2-13
snew cellfun(@(c) strrep(c, ' ', char(0x2007)), s, 'UniformOutput', false)
should probably be
snew = cellfun(@(c) strrep(c, ' ', char(0x2007)), s, 'UniformOutput', false);
Les Beckham
2023-2-14
Of course it should. Thanks for catching that, Walter.
Les Beckham
2023-2-14
@Voss Thanks for the edit. I guess I should have done that. Mea culpa.
Star Strider
2017-7-12
0 个投票
Applying a particular format to a table does not appear to be an option. It does not even inherit the current format setting.
Walter Roberson
2017-7-12
0 个投票
If that is a table() object then you will need to convert the entries to strings using whatever formatting rules you find appropriate, such as '%10.7f'
table() objects have no format controls.
Matt O'Brien
2023-2-13
编辑:Matt O'Brien
2023-2-13
0 个投票
I have used a formatting string as indicated, but :
the numbers are now strings and ..... aligns left. This might be ok, but it also suppresses leading spaces, leaving a messy non aligned column of data.
Is there a workaround.
Can I replace leading spaces with some other non display char.
My own pet peeve here is that there is no option to set alignment within the screen layout gui. This is a basic requirement.

Here is a table. The third column is created using the following code to create a column and adding this column then to the table.
MyTestString(x,1) = sprintf('%10.3f',MyTable.MyDecimals(x));
Note... how the leading spaces are removed... thus rendering the exercise futile.
13 个评论
Matt O'Brien
2023-2-13
移动:Voss
2023-2-13
A crude work around is to replace leading spaces with underscores. Test data is slightly adjusted to show more variety of chars before and after the decimal point.

You might try using your original sprintf approach and then replace the spaces with non-breaking spaces.
uitable doesn't seem to be stripping those off but I couldn't seem to be able to force it to use a monospace font so it doesn't quite do the trick. Your screenshot above (with the underscores) looks better aligned than what I was able to get.
Here is how I did the replacement of the spaces:
s = compose('%10.4f', 50*rand(5,1));
snew = cellfun(@(c) strrep(c, ' ', char(0xa0)), s, 'UniformOutput', false)
Example output below (with different random data, obviously). Maybe it will work better for you.

Matt O'Brien
2023-2-13
移动:Voss
2023-2-13
Thanks.
I will use the underline option until I can find a better solution. Maybe I can find a better char to use.
It would be so simple to allow left, right or centre alignment on columns in the Gui builder. Right aligned would work with numbers converted to text with sprintf or similar.
I have designed, built and deployed large scale enterprise systems and am gobsmacked it is such a challenge to simply align decimal points in a table.
I was thinking of developing an app using MatLab, but have decided to use MatLab for prototyping ideas and will use other tools for my app development.
Matt O'Brien
2023-2-13
I cannot see how to accept this as the answer.... probably because I am not the original poster.
I regard this as a good solution for this issue.
My thanks.
Les Beckham
2023-2-13
Ah, of course. I had overloooked that fact. I think you can Vote for any answer, though.
Walter Roberson
2023-2-13
Is the question about aligning decimal points for command-line display of a table() object?
Is the question about aligning decimal points for variable browser display of a table() object?
Is the question about aligning decimal points for uitable() for traditional figures? If so does it need to be editable by the user? If it does, then is the user to be permitted to add additional decimal places that show up until the user presses return? If editing is to be permitted, then should the user be shown full decimal places when they click on the entry?
Is the question about aligning decimal for uitable() for uifigure() [such as for App Designer]? If so does it need to be editable by the user? (and same questions about editing as for traditional figures -- uitables for uifigure have different implementing technology)
Matt O'Brien
2023-2-13
编辑:Matt O'Brien
2023-2-13
It is for Table Display of a column of numbers in App Designer.
The solution for me is to transfer an sprintf string to the table, replacing spaces with 'Figure Space' char as suggested by Les Beckham above.
Walter Roberson
2023-2-13
You can use uistyle() to create a style with 'FontName' and a monospaced font. You can then addStyle to add the style to a uitable (or parts of a uitable).
If you are changing the entries to character anyhow, then you could also code the entries in terms of latex or html markup; see uistyle for the supported controls. Unfortunately at the moment I do not see a way to apply an html table format to multiple cells.
You could also hypothetically resort to using uihtml
Matt O'Brien
2023-2-13
The Table is used for display purposes, so will not be edited.
I need to investigate the uistyle further. I had a quick look, but could not find an option forleft, centre or right alignment, Right alignment would be a simple solution in my case.
Also, I saw that certain format generic types could be applied, but none of the format types listed would yield the desired alignment (such as bank, etc).
Converting the number to string and replacing spaces with 'Figure Space' seems to work.
I will investigate Uistyle further when I get the chance,
uistyle itself does not offer alignment, but it does offer monospace font.
If you create the numbers with a fixed-width format such as %5.2f then the source of non-alignment would be
- that spaces are given different width than digits -- fixed by using monospace
- that in theory digits could be different widths -- fixed by using monospace
- that period is a different width -- fixed by using monospace
- format is not wide enough to hold all of the digits -- fixed by increasing width
fprintf('%7.3f\n', [pi/1000, pi/100, pi/10, pi, pi*10, pi*100])
Matt O'Brien
2023-2-13
Thank you.
Ps. I have not used monospace fonts since I coded in assembler and built apps for mainframes, so I understand these fonts very well.
Matt O'Brien
2023-2-14
Thanks for the links. They will be useful when I explore style options later.
In the interim, I have replaced leading spaces with the 'Figure Space' char (and added a Figure Space after the number to provide spacing before the right hand border) to achieve the following.

I see I need to tidy up other alignment elements (such as the Column Titles and the Count Column)... but can do that when I have more spare time.
Again, I appreciate all the valueable ideas to help me solve this thorny issue.
类别
在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!