How to search and sort strings from a structure efficently?
조회 수: 5 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
Stephen23
2017년 5월 11일
편집: Stephen23
2017년 5월 11일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!