split a string by a set of pre-defined number of characters rather than any delimiter

5 ビュー (過去 30 日間)
How to
1) First split a string by the number of chacters to five substrings, where sub string 1 has 19 characters and the rest of 4 substrings have only 13 chacaters?
Please note that I do not want to split using the tab or whitespace.
2) Remove any non dnumeric values (e.g. "- Psat" in the following example) and any white spaces from the resulting substrings?
Example: if S1 is my original string
S1= ' 961.666 - Psat 1.0000 45.0971 3.6734'
I want to have the final substrings as
'961.666', '1.0000', '45.0971' and '3.6734'
  1 件のコメント
Rik
Rik 2019 年 11 月 8 日
You can use a regular expression. I'm on mobile, so writing the regex myself is a bit challenging.

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

採用された回答

Stephen23
Stephen23 2019 年 11 月 8 日
You could do that using regular expressions:
>> S1 = ' 961.666 - Psat 1.0000 45.0971 3.6734';
>> C = regexp(S1,'(.{19})(.{13})(.{13})(.{13})(.{13})','tokens','once');
>> C = regexprep(C,'[^\d\.]+','')
C =
'961.666' '1.0000' '' '45.0971' '3.6734'
Although I suspect that you probably want something more like this:
>> C = regexp(S1,'\d+\.\d*','match')
C =
'961.666' '1.0000' '45.0971' '3.6734'

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 11 月 8 日
Just index, and strtrim()

カテゴリ

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