Displaying percentage signs in a table

Is there any way to display percentage signs in table data? for something like:
table = table(...
a_eff*100,...
b_eff*100,...
c_eff*100,...
d_eff*100);
eff_table.Properties.VariableNames = {...
'a_output_efficiency'...
'b_output_efficiency'...
'c_efficiency'...
'd_efficiency'};
disp(eff_table)
and have each number be followed by a percentage sign?

 Risposta accettata

Unfortunately, not in R2018b. (Thank you for providing that information.)
From the documentation:
‘Starting in R2019b, you can specify table variable names that are not valid MATLAB® identifiers. Such variable names can include spaces, non-ASCII characters, and can have any character as the leading character. When you access such a variable name, enclose it quotation marks.’
So if you upgrade to R2019b or later, this will work:
T1 = array2table(rand(4));
T1.Properties.VariableNames = {...
'a_output_efficiency %'...
'b_output_efficiency %'...
'c_efficiency %'...
'd_efficiency %'};
I verified that it does (in R2020a).
.

4 Commenti

Ah well, thank you for the answer. I suppose I could make a workaround by converting the number to a string with the percentage sign and displaying the table as an array of strings rather than doubles.
For future reference, here's what I did to have the desired output:
Convert each percentage to a string, using strcat to concatenate the percentage number with the sign, add to a 2D cell array between {}:
output_efficiency = {...
strcat(num2str(a_eff*100),'%') ...
strcat(num2str(b_eff*100),'%')...
strcat(num2str(fc_eff*100),'%')...
strcat(num2str(d_eff*100),'%')};
Convert to table using cell2table:
eff_table = cell2table(output_efficiency);
Set table variable names if desired (this is where it would be nice to add nonvalid identifiers in pre-2019b versions, no pesky _ required) and display table:
eff_table.Properties.VariableNames = {...
'a_output_efficiency'...
'b_output_efficiency'...
'c_output_efficiency'...
'd_output_efficiency'};
disp(eff_table)
As always, my pleasure!
Congratulations ion the work-around!
Stephen23
Stephen23 il 22 Lug 2020
Modificato: Stephen23 il 22 Lug 2020
@Star Strider: it is not clear to me how your answer relates to the question. You wrote about table variable names, but the original question and later examples are about how to append percent symbols to the data itself.
Can you please explain how setting the variable names changes the format of the data in the table ? Perhaps you could show an actual MWE that we can try, which demonstrates how changing the variable names (as you write) will change the display format of the table data (as the question requests).
I would also find such formatting very useful, but perhaps I just missed something obvious in the documentation.

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by