フィルターのクリア

Strange issue encountered using textscan and cell arrays

1 回表示 (過去 30 日間)
Peter Ryan
Peter Ryan 2016 年 10 月 14 日
コメント済み: Star Strider 2016 年 10 月 14 日
When I run the following snippet of code:
str = '(3.3940269999999999,1,0) (0,3.3940269999999999,0) (0,0,2.024902)';
format = '(%f, %d, %d) (%d, %f, %d) (%d, %d, %f)';
A = textscan(str, format);
matrix = [A{1,1}, A{1,4}, A{1,7} ; A{1,2}, A{1,5}, A{1,8}; A{1,3}, A{1,6}, A{1,9}]
I get the following output matrix =
3 0 0
1 3 0
0 0 2
Why were the floating point numbers converted to integers?

採用された回答

Star Strider
Star Strider 2016 年 10 月 14 日
When I run a slightly modified version of your code (because format is a function that defines the format displayed in the Command Window and Tooltips), I get:
format_string = '(%f, %d, %d) (%d, %f, %d) (%d, %d, %f)';
A = textscan(str, format_string);
matrix =
3×3 int32 matrix
3 0 0
1 3 0
0 0 2
That is likely because of the '%d' format descriptors. Convert them to '%f':
format_string = '(%f, %f, %f) (%f, %f, %f) (%f, %f, %f)';
to get:
matrix =
3.394 0 0
1 3.394 0
0 0 2.0249
  3 件のコメント
Peter Ryan
Peter Ryan 2016 年 10 月 14 日
Thanks!
Star Strider
Star Strider 2016 年 10 月 14 日
My pleasure!

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

その他の回答 (1 件)

Eamon
Eamon 2016 年 10 月 14 日
I think if you use the format function like below before the rest of your code, it will show the original floating point numbers.
format long
  1 件のコメント
Eamon
Eamon 2016 年 10 月 14 日
Nevermind, I tried this and it doesn't work.

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by