How to search and sort strings from a structure efficently?

6 visualizaciones (últimos 30 días)
Ibro Tutic
Ibro Tutic el 11 de Mayo de 2017
Editada: Ibro Tutic el 11 de Mayo de 2017
Lets say I have one structure with file names as character arrays (strings). Some file names have Z1P in them, Z1G, or Z1K. Other's might not. What I am trying to do is sort this structure (Called Dat.standard.name) by these files names, into a separate structure with the strings that I am looking for as fields. So from the Dat.standard.name, the unsorted data gets sorted into a structure called Sort.standard.Z1P for any files that have Z1P in them, Sort.standard.Z1G for any files that have Z1G in them and so on. Any ideas on how to go about this in the most efficient manner?

Respuesta aceptada

Stephen23
Stephen23 el 11 de Mayo de 2017
Editada: Stephen23 el 11 de Mayo de 2017
Dat.standard(1).name = 'x';
Dat.standard(2).name = 'xZ1P';
Dat.standard(3).name = 'x';
Dat.standard(4).name = 'xZ1G';
Dat.standard(5).name = 'x';
Dat.standard(1).data = 1;
Dat.standard(2).data = 2;
Dat.standard(3).data = 3;
Dat.standard(4).data = 4;
Dat.standard(5).data = 5;
C = {'Z1P','Z1G','Z1K'};
S = Dat.standard;
fun = @(s)cellfun('isempty',regexp(s,C,'once'));
idx = cellfun(@(s)any(~fun(s)),{S.name});
Srt.standard = S(idx);
Where Srt.standard contains a structure array which is a subset of Dat.standard, where the names contain any of the strings in C.
Note: in general you should store your data in the simplest array/structure possible. In this case nested structures do not make working with your data simpler, but actually make it more complicated. It might be worth considering if you could simplify the data organization somehow.
  1 comentario
Ibro Tutic
Ibro Tutic el 11 de Mayo de 2017
Editada: Ibro Tutic el 11 de Mayo de 2017
The reason for the nested structure is because I have these same files for multiples tests with different parameters. The intention is to get a single structure, with the data from each of the the Z1G, Z1P, etc files for each test. I plan on saving these structures off as MAT files and then all of the data is available in one easy to find place. If you have any recommendations for methods that may be easier I would be happy to try them.
The data itself is stored in these files. I am not working with any data besides the filenames themselves. My intention was to sort all of the data first, and then pull it into the structure. To avoid working with millions of numbers and moving them from structure to structure.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures 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