How do I convert numbers of the form "12,109,987" stored in strings

1 回表示 (過去 30 日間)
Asma
Asma 2022 年 6 月 19 日
編集済み: Ibrahim Mohammed Wajid 2022 年 6 月 19 日
I need to work on numbers of the form "12,109,987" which are stored in strings so taht I can perform mathematical operations on it.
If I use str2num("12,109,987", it just gives three different numbers present on either side of commas [12 109 987] as output and it is difficult to work with them.
Thanks in advance.

採用された回答

Ibrahim Mohammed Wajid
Ibrahim Mohammed Wajid 2022 年 6 月 19 日
編集済み: Ibrahim Mohammed Wajid 2022 年 6 月 19 日
you can use str2double function instead of str2num
str2double performs something similar to below operation if in case you need to implement yourself for different cases in future.
Refer this documentation for more info : str2double function
where s is your string.
[~,len] = size(s)
p = ""
for i = 1:len
if s(i)~= ','
p = p + s(i);
end
end
p = str2num(p); % p contains number from string as per your requirement
  1 件のコメント
Asma
Asma 2022 年 6 月 19 日
Its working out.
Thanks for the detailed explanation.

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

その他の回答 (1 件)

Simon Chan
Simon Chan 2022 年 6 月 19 日
use function str2double instead of str2num

カテゴリ

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

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by