Efficient means to extract individual elements from a structure with fields comrised of 6x1 cells

5 views (last 30 days)
I have a structure with each field a 6x1 cell array such as Xmin:{6x1 cell} where the cell contents appear below:
6×1 cell array
{'m'}
{'cm'}
{[0]}
{[0]}
{[1]}
{[1]}
These are simple upper/lower limits, conversion factors and units which permit going back and forth between sets units, an analysis engine that iexpresses in meters and radians while the GUI and output might be in a combination of English units and degrees. I can easily index into the cell components with the appropriate integer but when coding that is error prone. Let's say I coud us a constant, ie2i, as the index into the 5th element which converts from external to internal units.
Options to avoid using integer indices:
  1. global constants of the form ie2i, ie2e, etc. I've gone to some difficult avoiding global anything so this is a non-starter
  2. I could put the numeric elements in one table, the character arrays in another, and use row names to access these elements. I don't know about efficiency. I wouldn't say it's too late to change to table structures based on where I am in development.
  3. create a set of functions to access individual elements of the form: [<indexed element>] = getext2intconversion(<structure name>, ... < field name>). This would require six one-line public functions in one of the Apps, one for each of the cell array elements.
Is there a more elegant way to accomplish this, something I'm missing?
  1 Comment
Stephen23
Stephen23 on 22 Aug 2021
@Richard: it is not clear to me what the actual problem is (and judging by the lack of other responses, no one else understands your explanation either). Your "options" list is confusing because you mix together very dispirate topics.
Please provide a concrete example, if possible a complete MWE.
"Is there a more elegant way to accomplish this, something I'm missing?"
I have read your question five times and cannot find an explanation of what "this" is.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 22 Aug 2021
Is this possibly what you mean:
% 6×1 cell array
Xmin1 = {'m';'cm'; [0];[2];[3];[4]}
Xmin2 = {'m';'cm'; [5];[6];[7];[8]}
% Make the cell array a field of the structure called str.
% Make the structure be an array of 2 structures.
str(1).Xmin = Xmin1;
str(2).Xmin = Xmin2;
% Convert to an array.
a = struct2array(str)
% Extract row 5 only
%row5 = a(5, :) is a 1x2 cell array, so we need to convert it to numbers.
row5 = cell2mat(a(5, :)) % A 1x2 double array, [3, 7]

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!