time | Calls | line |
---|
| | 1 | function s = rmfield(s,field)
|
| | 2 | %RMFIELD Remove fields from a structure array.
|
| | 3 | % S = RMFIELD(S,FIELD) removes the field specified by FIELD from the
|
| | 4 | % m x n structure array S. For example, S = RMFIELD(S,'a') removes the field
|
| | 5 | % 'a' from S. The size of input S is preserved.
|
| | 6 | %
|
| | 7 | % S = RMFIELD(S,FIELDS) removes more than one field at a time when FIELDS
|
| | 8 | % is a string, character array or cell array of character vectors. The
|
| | 9 | % changed structure is returned. The size of input S is preserved.
|
| | 10 | %
|
| | 11 | % See also SETFIELD, GETFIELD, ISFIELD, FIELDNAMES.
|
| | 12 |
|
| | 13 | % Copyright 1984-2017 The MathWorks, Inc.
|
| | 14 |
|
| | 15 | %--------------------------------------------------------------------------------------------
|
| | 16 | % handle input arguments
|
| | 17 |
|
| | 18 |
|
< 0.001 | 20 | 19 | if ~isa(s,'struct')
|
| | 20 | error(message('MATLAB:rmfield:Arg1NotStructArray'));
|
< 0.001 | 20 | 21 | end
|
| | 22 |
|
< 0.001 | 20 | 23 | field = convertStringsToChars(field);
|
| | 24 |
|
< 0.001 | 20 | 25 | if iscell(field) && isempty(field)
|
| | 26 | % No fields to remove
|
| | 27 | return
|
< 0.001 | 20 | 28 | end
|
| | 29 |
|
< 0.001 | 20 | 30 | if ~ischar(field) && ~iscellstr(field)
|
| | 31 | error(message('MATLAB:rmfield:FieldnamesNotStrings'));
|
< 0.001 | 20 | 32 | end
|
| | 33 |
|
< 0.001 | 20 | 34 | if ischar(field) && ~isrow(field)
|
| | 35 | % converts char matrix to cell-str but leave char vector alone
|
| | 36 | field = cellstr(field);
|
< 0.001 | 20 | 37 | end
|
| | 38 |
|
| | 39 | % get fieldnames of struct
|
< 0.001 | 20 | 40 | f = fieldnames(s);
|
| | 41 | % Determine which fieldnames to delete.
|
< 0.001 | 20 | 42 | if ischar(field) || isscalar(field)
|
| | 43 | % shortcut for single field.
|
< 0.001 | 20 | 44 | nonexistent = [];
|
< 0.001 | 20 | 45 | toRemove = strcmp(deblank(field),f);
|
< 0.001 | 20 | 46 | if ~any(toRemove)
|
| | 47 | nonexistent = find(~toRemove,1);
|
< 0.001 | 20 | 48 | end
|
| | 49 | elseif numel(f) < 100 && numel(field) < 10
|
| | 50 | % faster for small number of fields
|
| | 51 | [toRemove,nonexistent] = smallcase(f,field);
|
| | 52 | else
|
| | 53 | % faster for large number of fields.
|
| | 54 | [toRemove,nonexistent] = generalcase(f,field);
|
< 0.001 | 20 | 55 | end
|
| | 56 |
|
| | 57 | % If any given fields were not found, throw an error
|
< 0.001 | 20 | 58 | if ~isempty(nonexistent)
|
| | 59 | field = cellstr(field);
|
| | 60 | name = field{nonexistent};
|
| | 61 | % Make sure the given non-existent field name does not exceed max length
|
| | 62 | if length(name) > namelengthmax
|
| | 63 | error(message('MATLAB:rmfield:FieldnameTooLong', name));
|
| | 64 | else
|
| | 65 | error(message('MATLAB:rmfield:InvalidFieldname', name));
|
| | 66 | end
|
< 0.001 | 20 | 67 | end
|
| | 68 |
|
| | 69 | % convert struct to cell array
|
< 0.001 | 20 | 70 | c = struct2cell(s);
|
| | 71 |
|
| | 72 | % find size of cell array
|
< 0.001 | 20 | 73 | sz = size(c);
|
| | 74 |
|
| | 75 | % adjust size for fields to be removed
|
< 0.001 | 20 | 76 | sz(1) = sz(1) - nnz(toRemove);
|
| | 77 |
|
| | 78 | % rebuild struct
|
0.002 | 20 | 79 | s = cell2struct(reshape(c(~toRemove,:),sz),f(~toRemove));
|
| | 80 | %--------------------------------------------------------------------------------------------
|
< 0.001 | 20 | 81 | end
|
Other subfunctions in this file are not included in this listing.