MATLAB Answers

Multi-level structure sorting with empty initial structures

조회 수: 2(최근 30일)
D. Plotnick
D. Plotnick 2018년 4월 5일
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 ''. 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 = cat(1,, {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( = allfiles(ii).name;
else = cat(1,, {allfiles(ii).name});
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


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by