How I can modify the decimal numbers in a table's column?
조회 수: 95 (최근 30일)
이전 댓글 표시
I have a table with different values like: 0.5, -0.300, -99, 5, 0, and so on. I want to use the output of this table in a hydrological model so I need to have all values in 1 decimal (even integer values, like 5).
Here is an example:
-5.40000000000000
-2.20000000000000
-3.40000000000000
5
4
-99
0
Are what I have, But I need something like this:
-5.4
-2.2
-3.4
5.0
4.0
-99.0
0.0
I tried something like format long, format long g, and format bank, but they weren't helpful. So if anyone knows the solution I would be grateful if tells me.
Best regards
댓글 수: 0
채택된 답변
Chunru
2021년 8월 5일
a = randn(5,1)
str = sprintf('%.1f\n', a)
댓글 수: 6
Walter Roberson
2023년 11월 8일
format long g
Column = randn(5,1)
Table = table(Column)
Table.Column=round(Table.Column,1)
format short
Table
Looks to me as if it is working?
Walter Roberson
2023년 11월 8일
for exactly 2 decimal places, if you are using uitable() then set the uitable ColumnFormat property to 'bank'
Note that "bank" format rounds numbers. If you happened to be doing financial work then in some cases, floor is prefered to rounding; in such a situation you should use the somewhat-new round TieBreaker option.
추가 답변 (1개)
Awais Saeed
2021년 8월 5일
You want to display the numbers as shown or modify them? In case you want to modify them, you can use
number = -99.32345; % the number you want to modify
num_str = num2str(number); % convert it to string
[idx,~]=regexp(num_str,'[.]'); % search for decimal point
num_str(idx+2:end) = []; % delete everything from idx+2 till end
number = str2num(num_str) % convert result back to number
You can use this in a loop to modify all of your values.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Oceanography and Hydrology에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!