MATLAB Answers

Uitable Decimal Places How To?

127 views (last 30 days)
Al on 19 Jan 2015
Commented: Walter Roberson on 23 Aug 2018
currently some of my Uitable is decimal and some is scientific notation. I would like to make it all decimal and be able to control the number of decimal places. Anyone know how to set the output to decimal and be able to change the number of decimals in the context of my code? (below)
f = figure('Position',[1 1 500 250]);
%Row and column names
cnames = {'C1','C2'};
rnames = {'R1','R2','R3'...
'R4', 'R5', 'R6'...
'R9', 'R10'...
'R11' };
% create the data
d = [SmI SmD ; RmI RmD ; KmI KmD ; RthI RthD;QjthI QjthD;...
QjthISS QjthDSS;QpcI QpcD; QpcISS QpcDSS; QphI QphD;...
% Create the uitable
t = uitable('Data',d,...
% Set width and height
tableextent = get(t,'Extent');
oldposition = get(t,'Position');
newposition = [oldposition(1) oldposition(2) tableextent(3)...
set(t, 'Position', newposition);

  1 Comment

Jelthe on 23 Mar 2016
Nobdy? I got the same problem. It might just be writing '%.2f' somewhere. But i cant figure out where.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 23 Mar 2016
There is no control of the individual numeric entries other than...
ColumnFormat: "A string accepted by the format function, such as: 'short', 'bank', 'long'"
For anything more complex than that you need to convert the numeric entries to strings and display the strings.


Sign in to comment.

More Answers (1)

Allen Daubendiek
Allen Daubendiek on 23 Aug 2018
Building on Walter's answer, I found reformatting data in a cell string useful.
Try a variation of this (left pad each string to 9 characters):
d = cellstr( pad( string( d ), 9, 'left' ) );
uitable( gcf, 'Data', d, 'ColumnFormat',{'char', 'char'} );
This solution will break down at some point for picky users, but it improved my aesthetics in the short term.

  1 Comment

Walter Roberson
Walter Roberson on 23 Aug 2018
You might also want to switch the font to fixed width.
Another way of getting the padded cells is
d = sprintfc('%9.2f', NumericArray);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!