0

I need some help figuring how do i create a structure point.

I need two fields x and y then I want to create a function that calculates the distance between these two points.

What I have right now is:

function [ out ] = pointDist3( pointpair1, pointpair2)
%FUNCTION pointDist3 takes in any two pairs of points and 
% Calling sequence:
%   out = pointDist3(varargin)
%DEFINE VARIABLES
% minargs, maxargs = error checking variables 
% pointpair1 = structure containing fields for point 1: x1 and y1
% pointpair2 = structure containing fields for point 2: x2 and y2
% out = structure containing field distance

%CHECK FOR VALID INPUT
if ~isfield(pointpair1,'x', pointpair2, 'x' ) || ~isfield(pointpair1,'y', pointpair2, 'y')
    error('Input argument does not contain fields "x" and "y" for both points');
else

    out = sqrt((pointpair1.x-pointpair2.x)^2+(pointpair1.y-pointpair2.y)^2);

   end

end
Venom
  • 27
  • 7
  • What is the question? Does your code work or not? – David Nov 28 '14 at 00:18
  • I am trying to do this question: Define a structure point containing two fields, x and y. The x field will contain the x-position of the point, and the y field will contain the y-position of the point. Then write a function pointDist3 that accepts two points and returns the distance between the two points on the Cartesian plane. Be sure to check the number of input arguments in your function.

    No it does not work because it get stuck at the if statement.

    – Venom Nov 28 '14 at 00:30
  • I get this error 'Error using isfield' 'Too many input arguments.' 'Error in pointDist3 (line 28) if ~isfield(pointpair1,'x', pointpair2, 'x' ) || ~isfield(pointpair1,'y', pointpair2, 'y')' – Venom Nov 28 '14 at 00:30
  • Look up the `isfield` documentation, you are using it incorrectly. – David Nov 28 '14 at 00:41

1 Answers1

0

Thanks David for the fix! Here's the working version

function [ out ] = pointDist3( pointpair1, pointpair2)
%FUNCTION pointDist3 takes in any two pairs of points and 
% Calling sequence:
%   out = pointDist3(varargin)
%DEFINE VARIABLES
% minargs, maxargs = error checking variables 
% pointpair1 = structure containing fields for point 1: x1 and y1
% pointpair2 = structure containing fields for point 2: x2 and y2
% out = structure containing field distance

%CHECK FOR VALID INPUT
    if ~isfield(pointpair1,'x')|| ~isfield(pointpair2, 'x' ) || ~isfield(pointpair1,'y') || ~isfield(pointpair2, 'y')
        error('Input argument does not contain fields "x" and "y" for both points');
    else

        out = sqrt((pointpair1.x-pointpair2.x)^2+(pointpair1.y-pointpair2.y)^2);
    end 
end

TESTER:

   >> pointA.x = 3

pointA = 

    x: 3

>> pointA.y =4

pointA = 

    x: 3
    y: 4

>> pointB.x=4

pointB = 

    x: 4

>> pointB.y=5

pointB = 

    x: 4
    y: 5

>> pointDist3(pointA, pointB)

ans =

    1.4142
Venom
  • 27
  • 7