MATLAB: How to compare last n characters of many strings in matlab

compare last n characterscompare last n characters of stringstring compare

I have a huge number strings stored in cell named 'b'. some names of the signals inside this cell looks like this 'PECCalc_tqElFil2_VW_173', 'PECCtl_bDampCtl_VW_173', 'PECCtl_uDstRipplNrm_VW_171', 'PEC_kWaaDkMEoacAcF33ry86TG5B0BRjIXWbgnK1vt1D2Gc_171', 'time_152', 'time_173',
then I use string compare to get only signals starting with the name PEC using the following.
matcontent = load('w50_2_2.mat');
usefield = strncmpi(fieldnames(matcontent),'PEC',3);
b = fieldnames(matcontent);
matcontent_cell = struct2cell(matcontent);
selectedvariables = matcontent_cell(usefield);
so selectedvariables has all the signals starting only with PEC.
Similarly I want to filter out all the signals ending with _173. How do I do this? How can I use string compare to compare last n characters and segregate those signals? the length of these signals also vary. Please help me! Thanks in advance.

Best Answer

Use regexp, you can wrap it in a cellfun call to get the indices:
C = {...
'PECCalc_tqElFil2_VW_173'
'PECCtl_bDampCtl_VW_173'
'PECCtl_uDstRipplNrm_VW_171'
'PEC_kWaaDkMEoacAcF33ry86TG5B0BRjIXWbgnK1vt1D2Gc_171'
'time_152'
'time_173'
}
fun = @(str,pat)~cellfun('isempty',regexp(str,pat,'once'));
idxPEC = fun(C,'^PEC')
idx173 = fun(C,'\_173$')
returns these indices:
idxPEC =
1
1
1
1
0
0
idx173 =
1
1
0
0
0
1