How i can convert a number in table to 6-digit decimal?

70 vues (au cours des 30 derniers jours)
Troy Yoga
Troy Yoga le 22 Mar 2023
Commenté : Troy Yoga le 26 Mar 2023
Based on the table above,how to convert numbers in table to 6 digit decimal, thank you for your attention and solution

Réponse acceptée

Cameron
Cameron le 22 Mar 2023
Do you mean 6 total digits or 6 digits after the decimal? For 6 total digits, you can do this:
T = ([1:10;pi:pi:10*pi])';
MyArray = strings(size(T));
for row = 1:size(T,1)
for col = 1:size(T,2)
MyArray(row,col) = regexprep(sprintf('%#.7g',T(row,col)),'.$','');
end
end
disp(MyArray)
"1.00000" "3.14159" "2.00000" "6.28318" "3.00000" "9.42477" "4.00000" "12.5663" "5.00000" "15.7079" "6.00000" "18.8495" "7.00000" "21.9911" "8.00000" "25.1327" "9.00000" "28.2743" "10.0000" "31.4159"
For 6 digits after the decimal, you can do this:
T = ([1:10;pi:pi:10*pi])';
MyArray = strings(size(T));
for row = 1:size(T,1)
for col = 1:size(T,2)
MyArray(row,col) = sprintf('%.6f',T(row,col));
end
end
disp(MyArray)
"1.000000" "3.141593" "2.000000" "6.283185" "3.000000" "9.424778" "4.000000" "12.566371" "5.000000" "15.707963" "6.000000" "18.849556" "7.000000" "21.991149" "8.000000" "25.132741" "9.000000" "28.274334" "10.000000" "31.415927"

Plus de réponses (1)

Adam Danz
Adam Danz le 22 Mar 2023
Do you want to round your data to 6 decimal places or do you want to only display 6 decimal places?
Rounding data
There should be a good motivation to round your data since rounding reduces precision.
If the table T only contains numeric values, to round to 6 decimal places,
format long % for demo purposes; use "format default" to revert to default
T = table(randi(9,6,1),rand(6,1),rand(6,1))
T = 6×3 table
Var1 Var2 Var3 ____ _________________ _________________ 2 0.725338116901227 0.994765943138584 9 0.320618926475707 0.284367465241924 6 0.335310975596281 0.276336750461116 2 0.943554905674576 0.882825699220907 8 0.916217169287025 0.208771672709224 8 0.859710202417857 0.302971404253732
T = round(T,6)
T = 6×3 table
Var1 Var2 Var3 ____ ________ ________ 2 0.725338 0.994766 9 0.320619 0.284367 6 0.335311 0.276337 2 0.943555 0.882826 8 0.916217 0.208772 8 0.85971 0.302971
If there are non-numeric values, you must index the numeric columns,
T = table(randi(9,6,1),rand(6,1),rand(6,1),["A";"B";"C";"D";"E";"F"])
T = 6×4 table
Var1 Var2 Var3 Var4 ____ __________________ __________________ ____ 9 0.140640344325377 0.545690896520992 "A" 6 0.534564780303479 0.255537880321794 "B" 3 0.608246493062309 0.437895392704874 "C" 2 0.282577029861513 0.103822166775036 "D" 6 0.0932949799876582 0.0783210469773984 "E" 6 0.950524707254477 0.208341593890096 "F"
isnum = varfun(@isnumeric,T,'OutputFormat','uniform');
T(:,isnum) = round(T(:,isnum),6)
T = 6×4 table
Var1 Var2 Var3 Var4 ____ ________ ________ ____ 9 0.14064 0.545691 "A" 6 0.534565 0.255538 "B" 3 0.608246 0.437895 "C" 2 0.282577 0.103822 "D" 6 0.093295 0.078321 "E" 6 0.950525 0.208342 "F"
Limiting the display
One way to limit the display of all numeric values is to use the DisplayFormatOptions function (R2021a and later) or to set the format(style). However, options are limited to a predefined list of formats and numeric values that have fewer decimal places will not be padded. MATLAB does not show trailing 0s. This willl change the display of all numeric values in the command window.
The only way to show a specific number of decimal places with padded 0s is to convert the numeric values to strings (or characters or categoricals). This makes the numeric values more difficult to work with in MATLAB since they are no longer numbers. The for-loops @Cameron shared in the other answer is one way to achieve this. This method below only acts on columns of numeric data.
T = table(randi(9,6,1),rand(6,1),rand(6,1),["A";"B";"C";"D";"E";"F"])
T = 6×4 table
Var1 Var2 Var3 Var4 ____ __________________ _________________ ____ 8 0.756369931514242 0.884371198607867 "A" 3 0.0165155749564821 0.854212032598753 "B" 4 0.492091252123063 0.914375079398313 "C" 2 0.176764358391581 0.893269657248392 "D" 4 0.783188938813622 0.947915493887413 "E" 4 0.791837657976275 0.633317996462687 "F"
isnum = varfun(@isnumeric,T,'OutputFormat','uniform');
T = convertvars(T,isnum,'string'); % R2018b or later
T(:,isnum) = arrayfun(@(n) {sprintf('%.6f',n)},T{:,isnum})
T = 6×4 table
Var1 Var2 Var3 Var4 __________ __________ __________ ____ "8.000000" "0.756370" "0.884370" "A" "3.000000" "0.016516" "0.854210" "B" "4.000000" "0.492090" "0.914380" "C" "2.000000" "0.176760" "0.893270" "D" "4.000000" "0.783190" "0.947920" "E" "4.000000" "0.791840" "0.633320" "F"

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by