How can I find the location of a substring in a char array

31 ビュー (過去 30 日間)
Jonathan
Jonathan 2018 年 1 月 9 日
コメント済み: Jonathan 2018 年 1 月 9 日
I am trying to find the location of a substring in a char array
I have the variable comtext=7143x53 char each line is a string e.g.
HRV: Beat 1 : First beat in block
HRV: Beat 2; Interval 1 = 1034.93 ms (Normal)
HRV: Beat 3; Interval 2 = 1037.54 ms (Normal)
HRV: Beat 4; Interval 3 = 1007.84 ms (Normal)
HRV: Beat 5; Interval 4 = 972.177 ms (Normal)
HRV: Beat 6; Interval 5 = 970.848 ms (Normal)
start EO
HRV: Beat 7; Interval 6 = 945.21 ms (Normal)
HRV: Beat 8; Interval 7 = 962.898 ms (Normal)
I am trying to find the row number containing a particular beat (not all rows contain beats)
attempts so far include
>> idx = all(contains(comtext,'Beat 2'),2)
Undefined function 'contains' for input arguments of type
'char'.
k=strfind(comtext, 'Beat 2;')
Error using strfind
Input strings must have one row.
idx = all(ismember(comtext,'Beat 2'),2)
fails because I do not know in advance the entire text of the string containing the relevant beat, just the substring containing the beat number
any advice for finding the row whose string contains the relevant substring would be much appreciated
I am new to matlab, and am beginning to consider copying the relevant variables to excel to work with and then back to matlab for processing, but there must be a better solution
Thanks,
Jonathan
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 1 月 9 日
I predict you are using a version earlier than R2016b, but you will need to tell us which version you are using.
Jonathan
Jonathan 2018 年 1 月 9 日
Apologies, I am using 2013b

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

採用された回答

Stephen23
Stephen23 2018 年 1 月 9 日
編集済み: Stephen23 2018 年 1 月 9 日
C = [...
'HRV: Beat 1 : First beat in block '
'HRV: Beat 2; Interval 1 = 1034.93 ms (Normal)'
'HRV: Beat 3; Interval 2 = 1037.54 ms (Normal)'
'HRV: Beat 4; Interval 3 = 1007.84 ms (Normal)'
'HRV: Beat 5; Interval 4 = 972.177 ms (Normal)'
'HRV: Beat 6; Interval 5 = 970.848 ms (Normal)'
'start EO '
'HRV: Beat 7; Interval 6 = 945.21 ms (Normal) '
'HRV: Beat 8; Interval 7 = 962.898 ms (Normal '
];
>> X = ~cellfun('isempty',strfind(cellstr(C),'Beat 2'))
X =
0
1
0
0
0
0
0
0
0
>> find(X) % to get row number.
ans = 2
  1 件のコメント
Jonathan
Jonathan 2018 年 1 月 9 日
Thank you very much, works beautifully

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultirate Signal Processing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by