I have a structure with a large number of fields (a few thousand). I would like to keep a subset of these fields and eliminate the rest. E.g. S=(subset of S). I do this by first getting the fieldnames:
fields = fieldnames(S);
I then use a for loop and strcmp to find the variables I want to keep, setting up a "mask" boolean vector that is true for the field indices I want to keep, and false for those I want to remove. Finally, I remove the unneeded fields with rmfield:
S = rmfield(S,fields(~mask));
This method works fine, but is prohibitively slow. Using the profiler, I found that the call to rmfield is the culprit – the loop to find the proper fields to keep actually goes pretty quickly.
Is there a significantly faster way to accomplish the same thing that avoids using rmfield?
Best Answer