filter string in the string

25 ビュー (過去 30 日間)
Liger
Liger 2019 年 8 月 21 日
コメント済み: Liger 2019 年 8 月 28 日
Hello,
I am quietly new with matlab script.
I have a string as example.
str = 'this matlab is a good software, it is a version 9.4 which is equal to 2018a'
objective, I want to filter the number from that string ( so it is "9.4").
Note that I cannot see that number. All i want is to scan that number and show in my workspace.
so the scribt should read the text and identify the number of the version and show in my workspace.
I appreciate your help.
Regards,
LN
  2 件のコメント
Ted Shultz
Ted Shultz 2019 年 8 月 21 日
Do you not want to also get '2018'? What rule would the code use to exclude this?
Adam
Adam 2019 年 8 月 21 日
doc regexp
should help do this. It takes a bit of getting used to parameterising regular expressions though. There are likely simpler ways depending how robust you want it to be.

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

採用された回答

Stephan
Stephan 2019 年 8 月 21 日
編集済み: Stephan 2019 年 8 月 21 日
str = 'this matlab is a good software, it is a version 9.4 which is equal to 2018a'
str = char(str);
idx = find((uint8(str)>=48 & uint8(str)<=57) | uint8(str)==46 | uint8(str)==32);
res = split(string(str(idx))," ");
res(res=="")=[]
The second line makes sure that it also works with:
str = "this matlab is a good software, it is a version 9.4 which is equal to 2018a"
  3 件のコメント
Stephan
Stephan 2019 年 8 月 21 日
thank you for the hint!
Liger
Liger 2019 年 8 月 28 日
Thank you, i found the anwser so useful

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

その他の回答 (2 件)

Stephen23
Stephen23 2019 年 8 月 21 日
編集済み: Stephen23 2019 年 8 月 21 日
>> str = 'this matlab is a good software, it is a version 9.4 which is equal to 2018a';
>> out = regexp(str,'\d+\.\d+','match','once')
out = 9.4
  1 件のコメント
Liger
Liger 2019 年 8 月 28 日
Thank you

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


Guillaume
Guillaume 2019 年 8 月 21 日
編集済み: Guillaume 2019 年 8 月 21 日
Using a regular expression:
numbers = regexp(youstring, '\<\d*\.?\d+\>', 'once')
which will extract any number not attached to text. Allowed formats for number is 123, 123.45, .123

カテゴリ

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