time | calls | line |
---|
| | 1 | function t = rmfield(s,field)
|
| | 2 | %RMFIELD Remove fields from a structure array.
|
| | 3 | % S = RMFIELD(S,'field') removes the specified field from the
|
| | 4 | % m x n structure array S. The size of input S is preserved.
|
| | 5 | %
|
| | 6 | % S = RMFIELD(S,FIELDS) removes more than one field at a time
|
| | 7 | % when FIELDS is a character array or cell array of strings. The
|
| | 8 | % changed structure is returned. The size of input S is preserved.
|
| | 9 | %
|
| | 10 | % See also SETFIELD, GETFIELD, ISFIELD, FIELDNAMES.
|
| | 11 |
|
| | 12 | % Copyright 1984-2008 The MathWorks, Inc.
|
| | 13 |
|
| | 14 | %--------------------------------------------------------------------------------------------
|
| | 15 | % handle input arguments
|
| 12 | 16 | if ~isa(s,'struct')
|
| | 17 | error(message('MATLAB:rmfield:Arg1NotStructArray'));
|
| | 18 | end
|
| 12 | 19 | if ~ischar(field) && ~iscellstr(field)
|
| | 20 | error(message('MATLAB:rmfield:FieldnamesNotStrings'));
|
| 12 | 21 | elseif ischar(field)
|
| 12 | 22 | field = cellstr(field);
|
| 12 | 23 | end
|
| | 24 |
|
| | 25 | % get fieldnames of struct
|
| 12 | 26 | f = fieldnames(s);
|
| | 27 |
|
| | 28 | % Determine which fieldnames to delete.
|
| 12 | 29 | idxremove = [];
|
| 12 | 30 | for i=1:length(field)
|
| 12 | 31 | j = find(strcmp(field{i},f) == true);
|
| 12 | 32 | if isempty(j)
|
| | 33 | if length(field{i}) > namelengthmax
|
| | 34 | error(message('MATLAB:rmfield:FieldnameTooLong', field{ i }));
|
| | 35 | else
|
| | 36 | error(message('MATLAB:rmfield:InvalidFieldname', field{ i }));
|
| | 37 | end
|
| | 38 | end
|
| 12 | 39 | idxremove = [idxremove;j];
|
| 12 | 40 | end
|
| | 41 |
|
| | 42 | % set indices of fields to keep
|
| 12 | 43 | idxkeep = 1:length(f);
|
| 12 | 44 | idxkeep(idxremove) = [];
|
| | 45 |
|
| | 46 | % remove the specified fieldnames from the list of fieldnames.
|
| 12 | 47 | f(idxremove,:) = [];
|
| | 48 |
|
| | 49 | % convert struct to cell array
|
| 12 | 50 | c = struct2cell(s);
|
| | 51 |
|
| | 52 | % find size of cell array
|
| 12 | 53 | sizeofarray = size(c);
|
| 12 | 54 | newsizeofarray = sizeofarray;
|
| | 55 |
|
| | 56 | % adjust size for fields to be removed
|
| 12 | 57 | newsizeofarray(1) = sizeofarray(1) - length(idxremove);
|
| | 58 |
|
| | 59 | % rebuild struct
|
| 12 | 60 | t = cell2struct(reshape(c(idxkeep,:),newsizeofarray),f);
|
Other subfunctions in this file are not included in this listing.