Iterate over struct with length>1, with multiple fields
I have a struct with size>1, and multiple 16 fields.
Each fieldname corresponds to some quantity or property, and the struct stores these properties for 47 different items.
I’m trying to iterate the whole dataset. Preferrably, I’d like to iterate by fieldname and retrieve an array for each filename because within each field name the variable type is uniform.
To illustrate:
K>> teststruct = struct(‘name’, [‘Alice’, ‘Bob’, ‘Eve’], ‘age’, {24, 45, 35})
teststruct =
1×3 struct array with fields:
name
age
This shows up nicely as a table in the worspace browser.
However, if I iterate by fieldname, this goes wrong (spurious empty lines removed for readability):
K>> fnames = fieldnames(teststruct);
K>> teststruct.(fnames{1})
ans =
‘AliceBobEve’
ans =
‘AliceBobEve’
ans =
‘AliceBobEve’
What I wanted was an array of all the names, instead I get three answers, of compounded names.
If I do the same with the ‘age’ field, at least each answer contains only one age, but they’re still not in any kind of structure I could use in a code which does not know the size or field names of a struct it needs to deal with. In fact, if I assign the above to a variable, this happens:
K>> names = teststruct.(fnames{1})
names =
‘AliceBobEve’
Doing the same with the other field only gives me the first age. One correct piece of data at least, but still not at all what I wanted…
I tried applying the code I found here, which promises to print the contents of an entire struct, but the same happens: I only get the first value of everything.
I know that I could loop over the struct indices first, and access them like this:
value = testsruct(i).(fieldnames(j))
…but then I’d be getting them separately, one by one, instead of getting back the cell array (or any other kind of array) that was used to define the struct in the first place, which is way easier to deal with.
Is that possible somehow?I have a struct with size>1, and multiple 16 fields.
Each fieldname corresponds to some quantity or property, and the struct stores these properties for 47 different items.
I’m trying to iterate the whole dataset. Preferrably, I’d like to iterate by fieldname and retrieve an array for each filename because within each field name the variable type is uniform.
To illustrate:
K>> teststruct = struct(‘name’, [‘Alice’, ‘Bob’, ‘Eve’], ‘age’, {24, 45, 35})
teststruct =
1×3 struct array with fields:
name
age
This shows up nicely as a table in the worspace browser.
However, if I iterate by fieldname, this goes wrong (spurious empty lines removed for readability):
K>> fnames = fieldnames(teststruct);
K>> teststruct.(fnames{1})
ans =
‘AliceBobEve’
ans =
‘AliceBobEve’
ans =
‘AliceBobEve’
What I wanted was an array of all the names, instead I get three answers, of compounded names.
If I do the same with the ‘age’ field, at least each answer contains only one age, but they’re still not in any kind of structure I could use in a code which does not know the size or field names of a struct it needs to deal with. In fact, if I assign the above to a variable, this happens:
K>> names = teststruct.(fnames{1})
names =
‘AliceBobEve’
Doing the same with the other field only gives me the first age. One correct piece of data at least, but still not at all what I wanted…
I tried applying the code I found here, which promises to print the contents of an entire struct, but the same happens: I only get the first value of everything.
I know that I could loop over the struct indices first, and access them like this:
value = testsruct(i).(fieldnames(j))
…but then I’d be getting them separately, one by one, instead of getting back the cell array (or any other kind of array) that was used to define the struct in the first place, which is way easier to deal with.
Is that possible somehow? I have a struct with size>1, and multiple 16 fields.
Each fieldname corresponds to some quantity or property, and the struct stores these properties for 47 different items.
I’m trying to iterate the whole dataset. Preferrably, I’d like to iterate by fieldname and retrieve an array for each filename because within each field name the variable type is uniform.
To illustrate:
K>> teststruct = struct(‘name’, [‘Alice’, ‘Bob’, ‘Eve’], ‘age’, {24, 45, 35})
teststruct =
1×3 struct array with fields:
name
age
This shows up nicely as a table in the worspace browser.
However, if I iterate by fieldname, this goes wrong (spurious empty lines removed for readability):
K>> fnames = fieldnames(teststruct);
K>> teststruct.(fnames{1})
ans =
‘AliceBobEve’
ans =
‘AliceBobEve’
ans =
‘AliceBobEve’
What I wanted was an array of all the names, instead I get three answers, of compounded names.
If I do the same with the ‘age’ field, at least each answer contains only one age, but they’re still not in any kind of structure I could use in a code which does not know the size or field names of a struct it needs to deal with. In fact, if I assign the above to a variable, this happens:
K>> names = teststruct.(fnames{1})
names =
‘AliceBobEve’
Doing the same with the other field only gives me the first age. One correct piece of data at least, but still not at all what I wanted…
I tried applying the code I found here, which promises to print the contents of an entire struct, but the same happens: I only get the first value of everything.
I know that I could loop over the struct indices first, and access them like this:
value = testsruct(i).(fieldnames(j))
…but then I’d be getting them separately, one by one, instead of getting back the cell array (or any other kind of array) that was used to define the struct in the first place, which is way easier to deal with.
Is that possible somehow? struct, indexing MATLAB Answers — New Questions