from
UNEMPTY
by Andreas
Sets empty fields in a structure to a defined value.
|
| unempty(S,field,value) |
function [S,nc] = unempty(S,field,value)
% UNEMPTY Sets empty fields in a structure to a value
%
% This function is useful if numeric values from a structure
% array are to be extracted using the notion M = [S.field].
% UNEMPTY makes sure that empty matrices in the structure do
% not affect the size of M.
%
% S = unempty(C) sets all occurences of empty matrices in
% all fields of C to NaN.
%
% [S,nc] = unempty(C) reports the number of changed fields in
% nc.
%
% S=unempty(C,F) sets in a structure array all occurences
% of empty matrices for field F to NaN,
%
% S=unempty(C,F,value) sets all occurences to value.
%
% Example:
% S=[struct('f',1) struct('f',2) struct('f',[]) ];
% disp([S.f]) % size is different from S
% 1 2
% S = unempty(S);
% disp([S.f]) % size is the same as S
% 1 2 NaN
%
% 22 mar 05, aha, doc upgrade
% 14 Aug 02, aha, added count of changes
% andreas hartmann, 16 Feb 2002, v1.0
% number of changed fields
nc = 0;
if ~exist('value','var') | isempty(value)
value = NaN;
end
if ~exist('field','var') | isempty(field)
field = fieldnames(S);
elseif ischar('field')
field = { field }
end
for n = 1:length(field)
eval(['index = cellfun(''isempty'',{S.' field{n} '});']);
if sum(index)
nc = nc + sum(index);
eval(['[S(index).' field{n} '] = deal(NaN);']);
end
end
|
|
Contact us at files@mathworks.com