MATLAB Answers

Deleting the i-th entry from all fields of a struct

178 views (last 30 days)
I'm using a struct to hold data on a large sample of patients. Each field corresponds to an attribute or property of the patients. There are some patients whom I wish to remove from the dataset (from the struct). How can I do this?
For example, suppose I wish to remove the i-th patient from the struct. This means I need to delete the i-th values from each field of the struct. I don't know how to do that.
Here is how I'm creating my struct (from a 135737x11 matrix "M"):
patients = struct( 'id', M(:,11), ...
'PSAtests', M(:,1), ...
'PSAestimate', M(:,3), ...
'PSA', M(:,10), ...
'isActual', 1-M(:,4), ...
'age', M(:,5), ...
'cancer', M(:,6), ...
'cancerAge', M(:,7), ...
'biopsy', M(:,8), ...
'biopsyAge', M(:,9) );
Ideally, if I wished to remove all patients over the age of 80, I could do so by something like the following:
patients(patients.age>80) = []
Any ideas? Or if I'm going about this the wrong way, please suggest an alternative data handling strategy. I welcome any comments.

More Answers (1)

David Young
David Young on 2 Apr 2011
Have a look at the section in the manual on "Organizing Data in Structure Arrays", also available here.
You can change the organisation of your data to element-by-element, which probably makes sense for a collection of patient records, by creating an array of structures like this:
for k = 1:size(M,1)
patients(k) = struct( ...
'id', M(k,11), ...
'PSAtests', M(k,1), ...
'PSAestimate', M(k,3), ...
'PSA', M(k,10), ...
'isActual', 1-M(k,4), ...
'age', M(k,5), ...
'cancer', M(k,6), ...
'cancerAge', M(k,7), ...
'biopsy', M(k,8), ...
'biopsyAge', M(k,9) );
end
Then you can delete a set of records with a small modification of your proposed command:
patients([patients.age] > 80) = [];
(Note the square brackets.)
If you want to retain your existing data structure (perhaps to save memory), then I think that to delete a patient you have to write a loop which deletes the entry from each array in the structure in turn.

Community Treasure Hunt

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

Start Hunting!