I've got a script that I'm using to make comparison plots using data from two different sets of simulation runs. The data is stored in mat files in different folders. I use uigetdir to select the folders and then I search through the mat files in each folder to find matching test cases so I can make comparison plots for each matching test case.
I have a working solution. However, I don't understand why I have to go through two stages of conversion to finally get a cell array of strings containing the unique test case numbers so that I can sort them into numerical order instead of "ASCII dictionary order".
Here is the relevant code excerpt:
%%Find matching test cases in the two directories
o = dir(fullfile(dirold, '*.mat')); % 'old'
n = dir(fullfile(dirnew, '*.mat')); % 'new'
[C, iold, inew] = intersect({o.name}, {n.name}); % find common test case files in 'old' and 'new' directories
% Convert C to a sortable array of indices so the comparison plots will be in order by case number
cases = regexp(C, 'case(\d*)', 'tokens'); % extract case numbers (cell array of cells?)
for (iC = 1:length(cases)) % TODO: why do I need to do this step?
x(iC) = cases{iC}; %#ok<SAGROW>
endfor (iC = 1:length(cases)) % convert to cell array of strings
y(iC) = x{iC}; %#ok<SAGROW>end% Re-sort in numerical order instead of 'ASCII dictionary order'
[~,iy] = sort(str2num(char(y))); %#ok<ST2NM>
%%For each test case that is in both directories, make some comparison plots
for (iCase = iy') fprintf('\nFound matching test case ''%s''.\n', C{iCase}); od = load(fullfile(dirold, C{iCase})); % load 'old' data into 'od' struct
nd = load(fullfile(dirnew, C{iCase})); % load 'new' data into 'nd' struct
... <make the plots>end
My concern is this: why do I have to go through the two step process of creating the intermediate 'x' and 'y' so that I can finally get an sortable cell array of strings? Is there a way to do this that is more straightforward and less confusing? I don't understand why this is necessary and future users of this code (including myself) won't understand it either.
Any help to simplify this (or at least clarify what is going on and why this mess is necessary) would be much appreciated.
Note: the reason I want to do the 'numeric' sort is so that I get plots for the test cases in the order 1, 2, …, 9, 10, 11, … instead of 10, 11, … 19, 1, 21, 22, …, 2, 3, 4, …, etc. The mat files are named caseX_… where X is the test case number. By default, the dir command, and hence the intersect command, are sorting by "ASCII dictionary order" which is not what I want.
Best Answer