How to find a string in the following cell structure?
23 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have s{1}.name = 'abc', s{2}.name = 'xyz', ..., s{n}.name = 'something'. And I would like to know the existance of a strin mystr ='aaa' among these names, and its index. Is it possible to do without a for cycle?
0 comentarios
Respuestas (2)
madhan ravi
el 15 de Oct. de 2018
Editada: madhan ravi
el 15 de Oct. de 2018
index = find(strcmp({structname.field}, 'string')==1)
1 comentario
Stephen23
el 15 de Oct. de 2018
Editada: Stephen23
el 15 de Oct. de 2018
Storing lots of separate scalar structures in a cell array is less convenient than just using one non-scalar structure. You should replace the cell array of scalar structures with one single non-scalar structure, then your task is trivial:
c{1}.name = 'abc';
c{2}.name = 'xyz';
c{3}.name = 'something';
s = [c{:}] % convert cell of scalar structures to non-scalar structure.
x = strcmp('xyz',{s.name})
Even better would be to avoid the cell array entirely, and just use a non-scalar structure right from the start:
s(1).name = 'abc';
s(2).name = 'xyz';
s(3).name = 'something';
x = strcmp('xyz',{s.name})
Read more about how this works:
0 comentarios
Ver también
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!