MATLAB: Whats a fast method for keeping only a subset of fields from a structure

rmfieldstructures

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

Matlab's RMFIELD contains some inefficient time-wasters. Use the faster C-Mex function:
The actual values are created as shared data copied, such that the memory is used efficiently. The submission contains an M-Version also, which works equivalent to:
F = fieldnames(S);
D = struct2cell(S);
keep = ~mask; % As in your example
S = cell2struct(D(keep), F(keep));
But in a real implementation you have to consider struct arrays, removing all existing fields and other exceptions. Therefore I'd prefer the Mex.