textscan to parse string

4 ビュー (過去 30 日間)
Hugo SP
Hugo SP 2013 年 2 月 20 日
Hello,
I am trying to use textscan to parse an IP address to get 4 integers:
str = '48.2.81.56'
integers = textscan(str,'%u8',4, 'Delimiter', '.');
Why is this is not working correctly?
Thank you.

採用された回答

Walter Roberson
Walter Roberson 2013 年 2 月 20 日
Unfortunately, in at least some versions, textscan() keeps reading for numeric fields as long as what is in the input is characters that could be part of a number format. It does not pay attention to the delimiter until after it has done that.
So the 48.2.81.56 is read as "4 is a digit, 8 is a digit, decimal place is part of a floating point format, 2 is a digit, decimal point has already occurred in the number so the number must have ended, 48.2; now look, the decimal point is the delimiter. Okay 8 and 1 are digits, decimal point is part of a floating point format, 5 and 6 are digits, end of string so the number has ended, 81.56. Now convert the 48.2 and 81.56 into the %u8 format."
What you should do instead is
integers = uint8( str2double( regexp( str, '\.', 'split' ) ) )
  4 件のコメント
Hugo SP
Hugo SP 2013 年 2 月 20 日
Thank you for the answer. However, I think that this is a bug and should be corrected. I don't understand how '.' is part of an integer format, which is what I requested. If it was %f I would understand the error.
Walter Roberson
Walter Roberson 2013 年 2 月 20 日
Please submit a bug report; I do not work for Mathworks.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by