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

43 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 9 Jan 2018
I predict you are using a version earlier than R2016b, but you will need to tell us which version you are using.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 9 Jan 2018
Edited: Stephen23 on 9 Jan 2018
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!