round two decimal in table app designer

[app.UITableFilteredTable.Data]=Table_HistoricalTable_Struct(app.Eq);
x = [{90} repmat({60},1,size(app.UITableFilteredTable.Data,2)-1)];
app.UITableFilteredTable.ColumnWidth =x;
app.UITableFilteredTable.ColumnName ={};
i try to use : round(v*100)/100 in table field's but it doesn't work

 採用された回答

Voss
Voss 2023 年 8 月 4 日

1 投票

A random matrix:
M = rand(10,2);
A uitable showing that matrix (defaults to 4 decimal places):
figure()
uitable('Data',M);
Another uitable with the numbers rounded to two decimal places:
figure()
uitable('Data',round(M,2));
Another uitable with rounded data, using character vectors instead of numbers:
figure()
t = uitable('Data',compose('%.2f',M),'ColumnFormat',{'char','char'});
Since you are using a uifigure, you can also add a uistyle to the uitable that sets the HorizontalAlignment of the text to the right.
s = uistyle('HorizontalAlignment','right');
addStyle(t,s)

6 件のコメント

pipin
pipin 2023 年 8 月 4 日
i try this:
[app.UITableFilteredTable.Data]=Table_HistoricalTable_Struct(app.Eq);
x = [{90} repmat({60},1,size(app.UITableFilteredTable.Data,2)-1)];
app.UITableFilteredTable.ColumnWidth =x;
app.UITableFilteredTable.ColumnName ={};
app.UITableFilteredTable.ColumnFormat =repmat({'char'},1,size(app.UITableFilteredTable.Data,2)-1);
Warning: 'ColumnFormat' value has no effect when 'Data' value is a table array.
Warning: 'ColumnFormat' value has no effect when 'Data' value is a table array.
and where i can i write it ? (compose('%.2f',M),)
Voss
Voss 2023 年 8 月 4 日
Since your uitable's Data is a table, I think you'll have to change the data in the table variable itself.
T = Table_HistoricalTable_Struct(app.Eq);
str = compose('%.2f',T{:,2:end});
T = convertvars(T,T.Properties.VariableNames(2:end),'cell');
T{:,2:end} = str;
app.UITableFilteredTable.Data = T;
Note that now the data in columns 2-end of your uitable is character vectors instead of numbers, and if you convert it to numbers again, e.g., to do some calculation with it, you've lost precision beyond 2 decimal places. In other words, you are changing the actual data in the table, not just how it's displayed. I don't know of a way to change the display without changing the underlying data.
pipin
pipin 2023 年 8 月 4 日
thank
pipin
pipin 2023 年 8 月 5 日
hi,it's possibile to set HorizontalAlignment all element but not column n.2?
Voss
Voss 2023 年 8 月 5 日
Yes
t = app.UITableFilteredTable; % your uitable
s = uistyle('HorizontalAlignment','center') % 'center' or whatever you want them to be
addStyle(t,s,'column',[1 3:size(t.Data,2)]) % add style s to columns 1 and 3:end of uitable t
pipin
pipin 2023 年 8 月 5 日
thank you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import and Network Parameters についてさらに検索

質問済み:

2023 年 8 月 4 日

コメント済み:

2023 年 8 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by