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

32 次查看(过去 30 天)
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

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by