In a struct, how can you extract values for a vector of fieldnames

601 views (last 30 days)
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))
Temp = Temp = getfield(StateSpace, MyFieldNames)
I get an error saying, "Index exceeds matrix dimensions."
Thank you, Kevin
Kevin Bachovchin
Kevin Bachovchin on 22 Feb 2013
If it wasn't clear what I was asking, for example, assume 'x1' is one of the field names.
The command getfield(StateSpace,'x1') works fine and produces the desired result, but if I want to return values for multiple field names, is there a way to do this? Or do I have to extract the values one at a time using a for loop?

Sign in to comment.

Accepted Answer

Kevin Bachovchin
Kevin Bachovchin on 22 Feb 2013
MyFieldNames = fieldnames(MyStruct);
for i=1:3
MyValues(i,1) = getfield(MyStruct,MyFieldNames{i});

More Answers (5)

Michael Kane
Michael Kane on 16 Apr 2019
Kevin's answer can be condensed to oneline of code using cellfun
You could even make this into an anonymous function fieldvalues
fieldvalues = @(MyStruct)(cellfun(@(fieldName)(MyStruct.(fieldName)),fieldnames(MyStruct)))

Azzi Abdelmalek
Azzi Abdelmalek on 22 Feb 2013
Edited: Azzi Abdelmalek on 22 Feb 2013
if a is your struct array
rmfield(out,s(4:end)) % will remove fields from 4 to the end

Sign in to comment.

Jan on 22 Feb 2013
You could use a dedicated function like:
function Value = GetFields(S, Name)
if ischar(Name)
Value = S.(Name);
Value = []; % Or an error message
elseif iscellstr(Name)
Value = cell(size(Name));
for iName = 1:numel(Name)
Value{iName} = S.(Name{iName});
% Nothing or an error message?!
error('Input type not handled')
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 Comment
Kevin Bachovchin
Kevin Bachovchin on 22 Feb 2013
It's much easier to just use a for loop. It's weird that MATLAB only allows the getfield command to take a single field as an argument, but that problem can be overcome by using a for loop.

Sign in to comment.

Bart McCoy
Bart McCoy on 2 Jul 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)
% Extracts all values from a struct and returns them as a Kx1 cell array,
% mirroring the functionality of MATLAB's fieldnames(struc)
% 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);

Matteo Ceradini
Matteo Ceradini on 17 Jan 2022
Edited: Matteo Ceradini on 17 Jan 2022
Instead of the complex solutions proposed above, for me a simpler and cleaner way could be this one:
values = {my_struct(:).field_of_interest};
it will create a cell array containing only the values of interested - that then could be transformed to an array of the type of interest.
  1 Comment
Stephen23 on 18 Jan 2022
That is quite a different situation what the question asks about: your answer shows how to access one field of a (non-scalar) structure, whereas the question asks how to access multiple fields of a (scalar) structure.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by