In a struct, how can you extract values for a vector of fieldnames
Afficher commentaires plus anciens
Hello,
I have a struct called MyStruct. I can extract the field names with the command
MyFieldNames = fieldnames(MyStruct);
If I'm interested in extracting the values for first n of MyFieldNames, is there a way I can do this using the "getfield" command? If I try
Temp = getfield(StateSpace, MyFieldNames(1:n))
or
Temp = Temp = getfield(StateSpace, MyFieldNames)
I get an error saying, "Index exceeds matrix dimensions."
Thank you, Kevin
3 commentaires
Azzi Abdelmalek
le 22 Fév 2013
What do you mean by the first field?
Kevin Bachovchin
le 22 Fév 2013
Kevin Bachovchin
le 22 Fév 2013
Réponse acceptée
Plus de réponses (4)
Michael Kane
le 16 Avr 2019
Kevin's answer can be condensed to oneline of code using cellfun
cellfun(@(x)(MyStruct.(x)),fieldnames(MyStruct))
You could even make this into an anonymous function fieldvalues
fieldvalues = @(MyStruct)(cellfun(@(fieldName)(MyStruct.(fieldName)),fieldnames(MyStruct)))
Jan
le 22 Fév 2013
You could use a dedicated function like:
function Value = GetFields(S, Name)
if ischar(Name)
try
Value = S.(Name);
catch
Value = []; % Or an error message
end
elseif iscellstr(Name)
Value = cell(size(Name));
for iName = 1:numel(Name)
try
Value{iName} = S.(Name{iName});
catch
% Nothing or an error message?!
end
end
else
error('Input type not handled')
end
For large numbers of fieldnames is this more efficient:
function Value = GetFields(S, Name)
[dummy, iName, iS] = intersect(Name, fieldnames(S));
Value = cell(size(Name));
Data = struct2cell(S);
Value(iName) = Data(iS);
1 commentaire
Kevin Bachovchin
le 22 Fév 2013
Azzi Abdelmalek
le 22 Fév 2013
Modifié(e) : Azzi Abdelmalek
le 22 Fév 2013
Edit
if a is your struct array
s=fieldnames(a)
out=a
rmfield(out,s(4:end)) % will remove fields from 4 to the end
11 commentaires
Kevin Bachovchin
le 22 Fév 2013
Azzi Abdelmalek
le 22 Fév 2013
Modifié(e) : Azzi Abdelmalek
le 22 Fév 2013
Look at edited answer
Kevin Bachovchin
le 22 Fév 2013
Modifié(e) : Azzi Abdelmalek
le 22 Fév 2013
Azzi Abdelmalek
le 22 Fév 2013
Modifié(e) : Azzi Abdelmalek
le 22 Fév 2013
I am not following you. Have you tried the edited answer?
s=fieldnames(a)
out=a
rmfield(out,s(4:end)) % will remove fields from 4 to the end
Azzi Abdelmalek
le 22 Fév 2013
Once you get your three fields in the variable out use
result=reshape(cell2mat(struct2cell(out)),3,[])
Kevin Bachovchin
le 22 Fév 2013
Azzi Abdelmalek
le 22 Fév 2013
s=fieldnames(a)
out=a
out=rmfield(out,s(4:end))
result=reshape(cell2mat(struct2cell(out)),3,[])
Kevin Bachovchin
le 22 Fév 2013
Azzi Abdelmalek
le 22 Fév 2013
Look at this example
out=struct('v',{4},'y',{6},'z',{45})
result=reshape(cell2mat(struct2cell(out)),3,[])
Kevin Bachovchin
le 22 Fév 2013
Azzi Abdelmalek
le 22 Fév 2013
Modifié(e) : Azzi Abdelmalek
le 22 Fév 2013
result=struct2cell(out)
Bart McCoy
le 2 Juil 2021
There's just such an assymetry here. You have this great little function fieldnames() that accepts a struct and returns a cell array of names Where's the complementary fieldvalues() that accepts a struct and returns all struct values?
struct2cell() works great, but this question has been asked over & over for many years
Guess I'm writing my own.... just because: :)
function values = fieldvalues(struc)
% values = fieldvalues(struc)
%
% DESCRIPTION:
% Extracts all values from a struct and returns them as a Kx1 cell array,
% mirroring the functionality of MATLAB's fieldnames(struc)
%
%
% SEE ALSO:
% fieldnames(struc) Complementary function to fieldvalues
assert( isstruct(struc), sprintf("\n\nfieldvalues(struc)\n\tArgument 'struc' must be a structure\n\n"));
values = struct2cell(struc);
end
Catégories
En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!