1

Matlab has limitations in terms of how things can be numbered/indexed:

However, it might be common/appropriate/intuitive to number certain things using zero or negative numbers or non-integer numbers.

For example, storing more and more so-called spherical harmonics requires increasingly negative "indices".

Scaling and shifting those "indices" such that they are natural numbers, i.e. valid (cell-)array indices, has disadvantages (smaller negative indices or finer non-integer indices might appear later, requiring a recomputation of the scale or shift parameters and adjustment of the entire data structure; understanding the resulting data structure requires knowing the scale and shift parameters and requires "mental math" while looking at the data).

Is there a way without these disadvantages?

root
  • 1,812
  • 1
  • 12
  • 26
  • Either of your two solutions below are very inefficient memorywise, as each value is stored as a matrix, rather than a matrix element. They are also relatively expensive computationally. You are better off following the traditional ways of storing data that people have used for decades, not only in MATLAB but in all programming languages. – Cris Luengo Jan 08 '22 at 20:33
  • @CrisLuengo Thanks for pointing out the overhead. In cases where that's the computational bottleneck, you are right. Note that there are also other cases where it's *not* much slower but much more convenient. That's why Matlab has `struct` and `containers.Map` in the first place. – root Jan 08 '22 at 20:44
  • What advantage do these two solutions have over simple functions to map values or class methods (possibly overloading `subsindex`)? – beaker Jan 09 '22 at 15:28
  • @beaker Maybe overloading `subsindex` can use one of my two *universal* solutions "under the hood". Cool! But if you mean class methods or mapping values in an *situation-specific* way: 1. That needs to be coded separately for each case, depending on the mapping parameters (which might be even unknown in advance and subject to change and thus super-problematic, see question). 2. The usual advantages of `struct` and `containers.Map` (the reasons for which they exist), for example I think cell arrays can't be sparse in memory, so they have memory overhead if their entries are sparsely present. – root Jan 09 '22 at 19:12

3 Answers3

0

Numbers can be turned into valid field names for structure arrays for example using this function:

% Field names of Matlab structures:
%  - can't start with a digit, so we prepend 'number_',
%  - can't contain '-', so we replace it by 'minus',
%  - can't contain '+', so we replace it by 'plus' (for numbers such as -1e+20),
%  - can't contain '.', so we replace it by 'point'.
% For example, the number -0.5 turns into the string 'number_minus0point5'.
num2fieldname = @(x)(['number_' replace(replace(replace(num2str(x),'-','minus'),'.','point'),'+','plus')]);

For example, num2fieldname(-1.5e+20) yields 'number_minus1point5eplus20'.

To use them as field names, here for example using the "index" (2,-1):

mystructure.(num2fieldname(2)).(num2fieldname(-1)) = myobject

It won't work if rounding settings for num2str change.

root
  • 1,812
  • 1
  • 12
  • 26
0

In recent versions of MATLAB, the table data type allows arbitrary text as variable names.

Edric
  • 23,676
  • 2
  • 38
  • 40
-1

containers.Map might achieve what you want. Apparently it can store any objects as values. That seems to be undocumented.

root
  • 1,812
  • 1
  • 12
  • 26
  • https://stackoverflow.com/help/self-answer – root Jan 08 '22 at 19:57
  • `containers.Map` is documented here: https://www.mathworks.com/help/matlab/ref/containers.map.html - and specifically `'ValueType', 'any'` is listed there as storing any MATLAB data. – Edric Jan 10 '22 at 07:11
  • @Edric I mean that the first paragraph says "Values can be scalar or nonscalar arrays.", but values can also be other things than arrays. Thanks for the pointer to `'ValueType', 'any'` – root Jan 11 '22 at 20:38
  • In MATLAB, it's usual to refer to _everything_ as an "array", regardless of class or size. (This is because objects of _almost_ class in MATLAB can be concatenated into a non-scalar array). – Edric Jan 12 '22 at 08:44