Accessing elements in a multilevel structure

12 views (last 30 days)
I have a struct that contains many other structs. There are several patients. Each patient contains several lesions. And each lesion has various properties. So there are three nested structs. The overall patient struct, the lesion struct, and then the properties struct. For further clarification, here is a quick example. Patient(1) contains Lesion(1), Lesion(2), Lesion(3). Each Lesion has a size, type, etc associated with it.
I would like to know how to access the following information efficiently:
  • The sizes of all lesions. Such as: Patients(:).Lesion(:).size
  • The sizes of one patients lesions. Such as: Patient(1).Lesion(:).size
  • The sizes of all patients lesions of a particular type. Such as: Patients(:).Lesion(if Lesion.type==normal).size
  • The sizes of one patients lesion of a particular type. Such as: Patient(1).Lesion(if Lesion.type == normal).size
Thanks in advance.

Accepted Answer

James Tursa
James Tursa on 16 Feb 2015
Edited: James Tursa on 17 Feb 2015
The key to the following is that many operations (e.g., Patients.Lesion) return a comma separated list result, so one uses the [ ] nomenclature to concatenate them into a single array.
The sizes of all lesions:
Patients_Lesion = [Patients.Lesion];
sizes = [Patients_Lesion.size];
The sizes of one patient's lesions:
sizes1 = [Patients(1).Lesion.size];
For the others, can you clarify what the field Patients.Lesion.type is? E.g., is it a string, or is it numeric?
EDIT:
Using { } to concatenate the strings into cell arrays of strings. Then using cellfun to operate on that cell array of strings. It is easier to work with strings this way instead of using the [ ] as above with numeric data.
The sizes of all patients lesions of a particular type, e.g. 'normal':
types = {Patients_Lesion.type};
normal_types = cellfun(@(x)strcmp(x,'normal'),types);
normal_sizes = sizes(normal_types);
The sizes of one patients lesions of a particular type, e.g., 'normal':
types1 = {Patients(1).Lesion.type};
normal_types1 = cellfun(@(x)strcmp(x,'normal'),types1);
normal_sizes1 = sizes1(normal_types1);
EDIT #2:
I just remembered that strcmp works with cell arrays of strings directly. So you can replace the cellfun stuff above with simple calls to strcmp. E.g.,
normal_types = strcmp(types,'normal');
normal_types1 = strcmp(types1,'normal');
  4 Comments
Frieder Wittmann
Frieder Wittmann on 12 Dec 2019
To me, the issue here is, why is it possible to do
Patients_Lesion = [Patients.Lesion];
sizes = [Patients_Lesion.size];
but not
sizes = [[Patients.Lesion].size];
?
All the first version does is create an extra variable Patients_Lesion which I don't need and have to remove with
clear Patients_Lesion
So three times the amount of code.
I understand the problem that 'Patients(1).Lesion' might have a field called 'size' while 'Patients(2).Lesion' has no such field. But that does not explain while it's allowed do something with three lines of code and an extra variable that I can't do in one line.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!