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

조회 수: 2 (최근 30일)
Rafael Borobio Castillo
Rafael Borobio Castillo 2022년 8월 16일
편집: dpb 2022년 8월 17일
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