diff m-toolbox/classes/@cdata/minus.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m-toolbox/classes/@cdata/minus.m	Wed Nov 23 19:22:13 2011 +0100
@@ -0,0 +1,58 @@
+% MINUS implements subtraction operator for cdata objects.
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% DESCRIPTION: MINUS implements subtraction operator for two cdata objects.
+%
+% CALL:        a = d1-d2
+%              a = minus(d1,d2);
+%
+% <a href="matlab:utils.helper.displayMethodInfo('cdata', 'minus')">Parameters Description</a>
+%
+% VERSION:     $Id: minus.m,v 1.3 2011/04/08 08:56:34 hewitson Exp $
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+function varargout = minus(varargin)
+  
+  % get two data objects
+  d1 = copy(varargin{1}, nargout);
+  d2 = copy(varargin{2}, nargout);
+  
+  % check units
+  d1.yunits.simplify();
+  d2.yunits.simplify();
+  
+  if ~isempty(d1.yunits.strs) && ~isempty(d2.yunits.strs) && d1.yunits ~= d2.yunits
+    error('### When subtracting two data objects, the yunits must be the same');
+  end
+  
+  % add the data
+  dout = applyoperator(d1,d2,'minus');
+  
+  % handle units: since both are the same, we take the first non-empty unit
+  if isempty(d1.yunits.strs)
+    dout.yunits = d2.yunits;
+  else
+    dout.yunits = d1.yunits;
+  end
+  
+  % handle errors
+  err = @(err1, err2) sqrt(err1 .^2 + err2.^2);
+  if ~isempty(d1.dy) || ~isempty(d2.dy)    
+    if isempty(d1.dy)
+      d1.dy = zeros(size(d2.dy));
+    end
+    if isempty(d2.dy)
+      d2.dy = zeros(size(d1.dy));
+    end    
+    dout.dy = err(d1.dy, d2.dy);
+  else
+    dout.dy = [];
+  end
+
+  
+  % Single output
+  varargout{1} = dout;
+  
+end
+