find length of a field within structure and specific values

95 次查看(过去 30 天)
Hello,
I create a structure using the script below. I would like to know how can I find the length of each field and how can I find specific values within them?
Thank you very much
for ii = 1 : length(SplitDom);
Name_DomCoord = strcat('DomCoord',num2str(ii));
variable.(Name_DomCoord) = model.geom(Name_Geom).obj(SplitDom(ii)).getVertexCoord();
end

采纳的回答

Sruthi Geetha
Sruthi Geetha 2017-1-30
"getfield" function can be used to get the value of each field in the array. Refer the following link to know how to use the "getfield" function:
https://www.mathworks.com/help/matlab/ref/getfield.html
The length of each field in the array can be found by using "structfun" in combination with "numel" function.
For example:
clear s; % save old stuff!
s.a=1:10;
s.b=pi;
s.c=magic(3);
r=structfun(@numel,s)
%{
% r =
10
1
9
%}

更多回答(1 个)

George Abrahams
George Abrahams 2022-12-30
The problem with the structfun approach is that the array output relates to the structure's field order. If you don't want to rely on a particular field order and/or want to keep the field names for readability, you could use my fieldfun function on File Exchange / GitHub. It's like structfun, but it outputs a structure with the same fields as the input structure.
variable = struct( 'DomCoord1', 1, 'DomCoord2', [2 3], ...
'DomCoord3', [4 5 6] );
fieldfun( @numel, variable )
% ans = struct with fields:
% DomCoord1: 1
% DomCoord2: 2
% DomCoord3: 3

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by