How do I display a number as a percetage within a table?

2 次查看(过去 30 天)
I have table with number e.g., 90, and want to display these as 90% when using array2table.
Thanks, Rafael

回答(1 个)

dpb
dpb 2022-8-16
No can do...and leave as a number, anyways. tables are calculational tools and MATLAB is NOT a spreadsheet and the view of the table content is not intended as a formatted presentation-worthy display format.
If you think you simply must show the % sign, you'll have to do something like
>> array2table(string(compose('%3d%%',randi(100,10,1))),'VariableNames',{'Data'})
ans =
10×1 table
Data
______
" 5%"
" 91%"
" 95%"
" 50%"
" 49%"
" 34%"
" 91%"
" 37%"
" 12%"
" 79%"
>>
Of course, then you get the unavoidable double-quotes around the value to show you it is a string array -- if you use
>> tT.Char=char(tT.Data)
tT =
10×2 table
Data Char
______ ____
" 5%" 5%
" 91%" 91%
" 95%" 95%
" 50%" 50%
" 49%" 49%
" 34%" 34%
" 91%" 91%
" 37%" 37%
" 12%" 12%
" 79%" 79%
>>
and store as char() array, then it looks pretty but now an individual value isn't a single entitiy but a char() string array -
>> tT.Char(3)
ans =
' '
>> tT.Char(3,:)
ans =
' 95%'
>>
-- which shows you have to access it by both indices to retrieve the whole string. All in all, this is highly unlikely to be useful no matter how you try to mung it into something it isn't.
  1 个评论
dpb
dpb 2022-8-17
编辑:dpb 2022-8-17
ADDENDUM
Above said, I understand the desire -- have often wished one could set a format string for table variable display on a column basis. You can do it for certain variable classes like datetime or duration because they carry their display formatting information around with them, but "ordinary" numeric classes in particular get only the default global setting from format.
I've been dealing with a lot of financial data from the nonprofit college foundation for whom have being doing pro bono work last several years -- and there's a mix of both the financial and other data in some instances such that while format bank is good for the financial data, the SID that's a long integer is distracting to show with the two decimal places.
I don't know what it might do to performance; but I'd think the ability to have a display format property associated with a table shouldn't detract too much as it only comes into play when the data are shown on screen or (perhaps?) exported; doesn't matter a whit during calculations.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by