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

4 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 29 Jul. 2016
Bearbeitet: Stephen23 am 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 Kommentare
Tejaswini Ramesh
Tejaswini Ramesh am 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!
Stephen23
Stephen23 am 29 Jul. 2016
Bearbeitet: Stephen23 am 29 Jul. 2016
Using very basic array indexing:
C(idxPEC | idx173)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by