Failing to find a string on a cell of strings using strcmp
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Diogo Tecelão
 el 18 de Sept. de 2018
  
    
    
    
    
    Respondida: Christopher Wallace
      
 el 18 de Sept. de 2018
            Hello all,
I am currently trying to use strcmp to find a string on a cell string. However, it doesn't seem to work, and I can't find why. I guess it's a matter of data types incompatibility, but I can't figure it out.
Does anybody know?
subj = {'af_p040624_hr1' 'af_p040624_hr12' 'af_p040624_hr18' 'af_p040624_hr2' 'af_p040624_hr24' 'af_p040624_hr30' 'af_p040624_hr36' 'af_p040624_hr4'};
for i=1:length(subj)
   parts = strsplit(subj{i},'_');
   if isequal(subj{i}(1:3),'af_')
      record_groups{i} = 'POAF';
      record_groups_hrs{i} = ['POAF_',parts{3}];
   elseif isequal(subj{i}(1:4),'con_')
      record_groups{i} = 'Control';
      record_groups_hrs{i} = ['Control_',parts{3}];
   end
end
hrs = {'hr1','hr2','hr4','hr12','hr18','hr24','hr30','hr36','hr42','hr48'};
for h=1:length(hrs)
   curr_hr = hrs{h};
     %I want to find the index of the record_groups_hrs which contain EXACTLY something like '_hr1'.    
     curr_recs_idx = (strcmp(record_groups_hrs,['_',curr_hr]));
end
Many thanks!
0 comentarios
Respuesta aceptada
  Christopher Wallace
      
 el 18 de Sept. de 2018
        Try using 'regexp'
curr_recs_idx = regexp(record_groups_hrs,['_',curr_hr, '\>'])
0 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Characters and Strings en Help Center y File Exchange.
			
	Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

