Borrar filtros
Borrar filtros

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

4 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Stephen23
Stephen23 el 29 de Jul. de 2016
Editada: Stephen23 el 29 de Jul. de 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 comentarios
Tejaswini Ramesh
Tejaswini Ramesh el 29 de Jul. de 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 el 29 de Jul. de 2016
Editada: Stephen23 el 29 de Jul. de 2016
Using very basic array indexing:
C(idxPEC | idx173)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by