Deleting the i-th entry from all fields of a struct
10 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Daniel Underwood
2011-4-5
For the answer to this question, see the answer to the following question:
0 个评论
更多回答(1 个)
David Young
2011-4-2
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!