View your options:
Indexing into Cell Arrays
MATLAB indices start at 1, not 0. If you want to store your data in cell arrays, in the worst case scenario, you can always use the k + 1 index to index into the cell corresponding to the k-th identifier (k โฅ 0). In my opinion, using the last element as a โbase caseโ is more confusing. So you will have:
Res{1} = magic(4); %// Base case Res{2} = magic(5); %// Corresponds to identifier 1 ... Res{k + 1} = ... %// Corresponds to indentifier k
Access to fields in structures
Field names in structures cannot begin with numbers, but they are allowed to contain them starting with the second character. Therefore, you can build your structure as follows:
Res.c0 = magic(4); %// Base case Res.c1 = magic(5); %// Corresponds to identifier 1 Res.c2 = magic(6); %// Corresponds to identifier 2 %// And so on...
You can use the link to a dynamic field to access any field, for example:
k = 3; kth_field = Res.(sprintf('c%d', k)); %// Access field k = 3 (ie field 'c3')
I canโt say which alternative seems more elegant, but I believe that indexing in a cell should be faster than references to a dynamic field (but you can check this and prove that I'm wrong).
source share