Multi-level structure sorting with empty initial structures
2 ビュー (過去 30 日間)
古いコメントを表示
Hello all, I am attempting to look through a directory, and, based on parsing the filenames, split filenames into a number of categories and sub-categories in a structure format. Different functions will be applied to each category and each sub-category. An extremely crude example thought experiment is provided below, and while the structure method is what I originally gravitated to, maybe there is an alternative technique: suggestions welcome! (side note, there are some case and basic programming issues with my MWE...please ignore those.)
OK. So, lets say I have a series of raw data files with filenames like 'JaneDoe.woman.single.mat' or 'JonSmith.man.married.mat'. There are three characteristics: a name charstring, a 'man'/'woman' option, and a 'single','married' option. After using the dir command to obtain the contents of a directory, I want to sort the files into the 'men'/'women' categories, and then the 'married','single' subcategories. The files will then be loaded and functions applied in a boolean fashion; some functions apply to all 'men', some to all 'married', and some to 'single'&'women'.
So: two problems.
(1) I am having troubles concatenating empty sub-structures. For example, lets say I initialize a structure:
allFiles = dir(fullfile(pwd,'*.mat'));
files = struct(...
'men', struct('single',{},'married',{}),...
'women, struct('single',{},'married',{}));
iterating over the directory using ii as my index
files.men.single = cat(1, files.men.single, {allFiles(ii).name})
returns the error 'A dot name structure assignment is illegal when the structure is empty. Use a subscript on the structure.' Now, I tried using an isempty argument so that:
if isempty(files.men.single)
files.men.single = allfiles(ii).name;
else
files.men.single = cat(1,files.men.single, {allfiles(ii).name});
end
where I am just trying to set the initial file if its empty, or concatenate if it is not, and I get an 'Error using isempty: Not enough input arguments.' Using isempty on an empty cell works, but not on the empty cell of an empty structure.
So: problem 1 is "how do I initialize and then populate a structure+substructure+cellArray in an elegant way, without resorting to cramming the structures with garbage and then removing it at the end?"
(2) My structure+substructure format is due to my desired batching. I want to e.g. process all single men, then married men, then single women, then married women. The idea was to simply look at the number of elements in each category and subcategory, skip them if they are empty, and if not process them in one swell foop. However, there should be a better way of organizing this than the raw category/subcategory method I am using here. Is there a better way of doing this type of boolean processing than (A) brute force if (and/or) then statements (B) nested switch-case statements.
One can imagine this type of sorting going multiple layers deep. men/women + married/single + juggles/cannot juggle.
Cheers y'all. -Dan
0 件のコメント
回答 (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!