1 function varargout=
binaryunionstruct(leftStruct,rightStruct,fieldOpCell,leftFieldOpCell,rightFieldOpCell)
5 'there should be at least 2 arguments');
8 sizeStructVec=size(leftStruct);
9 if ~(isstruct(leftStruct)&&isstruct(rightStruct)&&...
10 mxberry.
core.checksize(leftStruct,rightStruct,sizeStructVec))
12 'first and second inputs should be structure arrays of the same size');
15 nDims=ndims(leftStruct);
19 elseif isa(fieldOpCell,
'function_handle')
20 fieldOpCell={fieldOpCell};
24 nOps=length(fieldOpCell);
27 rightFieldOpCell=
repmat({@(x)deal(x)},1,nOps);
29 leftFieldOpCell=
repmat({@(x)deal(x)},1,nOps);
34 if isa(leftFieldOpCell,
'function_handle')
35 leftFieldOpCell={leftFieldOpCell};
38 if isa(rightFieldOpCell,
'function_handle')
39 rightFieldOpCell={rightFieldOpCell};
45 [
'the third argument(fieldOpCell) should be a function handle',...
46 'or cell array of function handles']);
52 [
'the fourth argument(leftFieldOpCell) should be a function ',...
53 'handle or cell array of function handles']);
59 [
'the fifth argument(rightFieldOpCell) should be a function ',...
60 'handle or cell array of function handles']);
63 leftFieldList=fieldnames(leftStruct);
64 rightFieldList=fieldnames(rightStruct);
65 unitedFieldList=reshape(
union(leftFieldList,rightFieldList),[],1);
66 isLeftField=
ismember(unitedFieldList,leftFieldList);
67 isRightField=
ismember(unitedFieldList,rightFieldList);
68 isCommonField=isLeftField&isRightField;
70 nFields=length(unitedFieldList);
71 nFieldOps=length(fieldOpCell);
74 %
this cell array will contain result
75 resCell=cell(nArgout,nFields);
76 %
this field is a temporary variable containing result
for a specific field
77 resTmpCell=cell(nArgout,1);
80 fieldName=unitedFieldList{iField};
81 if isCommonField(iField)
82 for iFieldOp=1:nFieldOps
84 fieldOp=fieldOpCell{iFieldOp};
86 [resTmpCell{:}]=cellfun(fieldOp,...
87 reshape({leftStruct.(fieldName)},sizeStructVec),...
88 reshape({rightStruct.(fieldName)},sizeStructVec),...
89 'UniformOutput',
false);
94 elseif isLeftField(iField)
95 for iFieldOp=1:nFieldOps
96 leftFieldOp=leftFieldOpCell{iFieldOp};
98 [resTmpCell{:}]=cellfun(leftFieldOp,reshape(...
99 {leftStruct.(fieldName)},sizeStructVec),...
100 'UniformOutput',
false);
106 for iFieldOp=1:nFieldOps
107 rightFieldOp=rightFieldOpCell{iFieldOp};
109 [resTmpCell{:}]=cellfun(rightFieldOp,reshape(...
110 {rightStruct.(fieldName)},sizeStructVec),...
111 'UniformOutput',
false);
119 unitedFieldList=transpose(unitedFieldList);
121 varargout=cell(1,nArgout);
123 defCell=[unitedFieldList;resCell(iOut,:)];
124 varargout{iOut}=
struct(defCell{:});
127 resCell(:,iField)=cellfun(@(x,y)
cat(nDims+1,x,y),...
128 resCell(:,iField),resTmpCell,
'UniformOutput',
false);
135 [
'common fields cannot be processed by default, ',...
136 'binary operation required']);
140 isValid=iscell(fieldOpCell);
142 isAllFuncVec=cellfun(@(x)isa(x,
'function_handle'),fieldOpCell);
143 isValid=all(isAllFuncVec);
function throwerror(in msgTag, in varargin)
THROWERROR works similarly to built-in ERROR function in case when there is no output arguments but s...
function binaryunionstruct(in leftStruct, in rightStruct, in fieldOpCell, in leftFieldOpCell, in rightFieldOpCell)
BINARYUNIONSTRUCT applies a set of binary operations to the fields of two structures, each operation can return multiple results.
function auxcheckfuncell(in fieldOpCell)
function repmat(in inpArray, in varargin)
function binarynotdefined(in varargin)
function ismember(in leftVec, in rightVec, in varargin)
ISMEMBER - ismember implementation for arrays of any type.
function cat(in dimNum, in varargin)