0001 function distance = Distance(positions,reference,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 type = 'linear';
0033
0034 if nargin < 2 | mod(length(varargin),2) ~= 0,
0035 error('Incorrect number of parameters (type ''help <a href="matlab:help Distance">Distance</a>'' for details).');
0036 end
0037 if ~issamples(positions,'#1') && ~issamples(positions,'#2'),
0038 error('Incorrect positions - should be a vector (type ''help <a href="matlab:help Distance">Distance</a>'' for details).');
0039 end
0040 if ~isdvector(reference,'#1') && ~isdvector(reference,'#2'),
0041 error('Incorrect reference - should be a vector (type ''help <a href="matlab:help Distance">Distance</a>'' for details).');
0042 end
0043 if length(reference) ~= size(positions,2)-1,
0044 error('Positions and reference have incompatible sizes (type ''help <a href="matlab:help Distance">Distance</a>'' for details).');
0045 end
0046
0047
0048 for j = 1:2:length(varargin),
0049 if ~ischar(varargin{j}),
0050 error(['Parameter ' num2str(j+2) ' is not a property (type ''help <a href="matlab:help Distance">Distance</a>'' for details).']);
0051 end
0052 switch(lower(varargin{j})),
0053 case 'type',
0054 type = varargin{j+1};
0055 if ~isastring(type,'l','c'),
0056 error('Incorrect value for property ''type'' (type ''help <a href="matlab:help Distance">Distance</a>'' for details).');
0057 end
0058 otherwise,
0059 error(['Unknown property ''' num2str(varargin{j}) ''' (type ''help <a href="matlab:help Distance">Distance</a>'' for details).']);
0060 end
0061 end
0062
0063 distance = [];
0064 if isempty(positions), return; end
0065
0066 distance = positions(:,1);
0067
0068 if issamples(positions,'#1'),
0069 if type(1) == 'l',
0070 distance(:,2) = abs(positions(:,2)-reference(1));
0071 else
0072
0073 if max(positions(:,2)) > 1 || min(positions(:,2)) < 0,
0074 positions(:,2) = ZeroToOne(positions(:,2));
0075 warning('Positions should contain values in [0 1]. The data will now be transformed accordingly.');
0076 end
0077 distance1 = abs(positions(:,2)-reference(1));
0078 distance2 = 1-abs(positions(:,2)-reference(1));
0079 distance(:,2) = min([distance1 distance2],[],2);
0080 end
0081 else
0082 distance(:,2) = sqrt((positions(:,2)-reference(1)).^2+(positions(:,3)-reference(2)).^2);
0083 end