T = table([1.1; 2],[3.45; 1.2], 'VariableNames', {'a', 'b'}, 'RowNames', {'c', 'd'});
n_decimal = 2;
new_T = varfun(@(x) num2str(x, ['%' sprintf('.%df', n_decimal)]), T);
new_T.Properties.VariableNames = T.Properties.VariableNames;
new_T.Properties.RowNames = T.Properties.RowNames;
fprintf('Original table:\n')
T
fprintf('New table with %d decimal places:\n', n_decimal);
new_T
----------
You can also convert the numbers to string and format them with the "sprintf" command _before _adding them to the table:
data = [1.1 3.45; 2 1.2];
data_str = string(data);
for i = 1:numel(data_str)
data_str(i) = sprintf('%.2f',data_str(i));
end
t = array2table(data_str,'VariableNames',{'a','b'},'RowNames',{'c','d'})
Best Answer