how to compare last n characters of many strings in matlab?

4 views (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 29 Jul 2016
Edited: Stephen23 on 29 Jul 2016
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
  3 Comments
Tejaswini Ramesh
Tejaswini Ramesh on 29 Jul 2016
please also tell me how to get all the names of the signals when idx173 or idxPEC value is 1. Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!