Efficient means to extract individual elements from a structure with fields comrised of 6x1 cells
조회 수: 2 (최근 30일)
이전 댓글 표시
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:
- global constants of the form ie2i, ie2e, etc. I've gone to some difficult avoiding global anything so this is a non-starter
- 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.
- 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
Stephen23
2021년 8월 22일
@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.
"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.
답변 (1개)
Image Analyst
2021년 8월 22일
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]
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!