find length of a field within structure and specific values

93 vues (au cours des 30 derniers jours)
Nicolas
Nicolas le 26 Jan 2017
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

Réponse acceptée

Sruthi Geetha
Sruthi Geetha le 30 Jan 2017
"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
%}

Plus de réponses (1)

George Abrahams
George Abrahams le 30 Déc 2022
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

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by