フィルターのクリア

How to modify number of characters for numeric values in a cell array?

4 ビュー (過去 30 日間)
Calabrese
Calabrese 2017 年 7 月 15 日
編集済み: Calabrese 2017 年 7 月 15 日
Within a cell array, I would like to limit the number of characters allowed in each cell to 6. If the cell has more than 6 characters, I would like to remove the characters off of the end. i.e.
n_raw =
[ 0.023391]
[ 0.31236]
[ 0.0073958]
[ 0.6252]
[ 14.158]
[ 14.159]
[ 0.046701]
[ 0.095463]
to
n_raw =
[ 0.0233]
[ 0.3123]
[ 0.0073]
[ 0.6252]
[ 14.158]
[ 14.159]
[ 0.0467]
[ 0.0954]
  5 件のコメント
Calabrese
Calabrese 2017 年 7 月 15 日
Could I convert them to characters, reduce them, and then convert them back to numbers?
per isakson
per isakson 2017 年 7 月 15 日
Why not try?
You have to distinguish between internal representation and what's displayed.
>> cac{1} = 1/3;
cac{2} = 33.3;
cac
cac =
[0.3333] [33.3000]
>> format long
>> cac
cac =
[0.333333333333333] [33.299999999999997]

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2017 年 7 月 15 日
Under the assumption that each entry in the cell is a column vector, and cell itself is a column vector, but not assuming that each entry is a scalar:
n_raw = {
0.023391
0.31236
0.0073958
0.6252
14.158
-14.159
0.046701
0.095463 };
rv = vertcat(n_raw{:});
new_n_raw = mat2cell( floor(abs(rv) * 10000) / 10000 .* sign(rv), cellfun(@length, n_raw), 1);
Note, however, that the only fractions that can be represented exactly in 4 decimal digits are the integer multiples of 1/16 (e.g., 5/16 or 6/16 = 3/8 can be represented exactly in 4 decimal digits, but 11/32 cannot be represented exactly in 4 decimal digits.) In all other cases, the rounding to 4 decimal digits will only look to work. For example, what looks like 0.023391 rounded to what looks like 0.0233 would actually become 0.02330000000000000126565424807267845608294010162353515625

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by