How to find a structure point with two fields x and y and then find the distance between two points
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Iris
am 27 Nov. 2014
Kommentiert: Deven Kyser
am 5 Apr. 2022
I am lost how to create a structure point so I use it two create two points and then find the distance between these two points.
The instructions that was given to me was:
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 dist3 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.
Here is what I have so far.
function [ out ] = pointDist3( varargin )
%FUNCTION pointDist3 takes in any two pairs of points and
% Calling sequence:
% out = pointDist3(varargin)
%DEFINE VARIABLES
% minargs, maxargs = error checking variables
minargs = 4;
maxargs=8;
nargchkin(minargs,maxargs);
%Initalize values
jj = 0;
linespec = '';
% Get the x and y values, making sure to save the line
% specification string, if one exists.
for ii = 1:nargin
% Is this argument an [x,y] pair or the line
% specification?
if ischar(varargin{ii})
% Save line specification
linespec = varargin{ii};
else
% This is an [x,y] pair. Recover the values.
jj = jj + 1;
x(jj) = varargin{ii}(1);
y(jj) = varargin{ii}(2);
end
end
% Plot
end
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 27 Nov. 2014
Iris - I think the first step is to define the point structure/object as something like
pointA.x = 12;
pointA.y = 32;
which would represent the point (12,32) in the 2D Cartesian plane, with x and y being your two fields. Your distance function, dist3, would take two of these structures as its inputs, and you would then calculate the distance between the two points.
Since this is a homework assignment, try to incorporate the above into your code. When you review your updated function, ask yourself if the line specification logic is necessary (it isn't all that clear to me what this check is for). Use nargin to determine if you have the correct number of inputs, and isfield to ensure that both inputs have x and y fields.
4 Kommentare
Geoff Hayes
am 28 Nov. 2014
Bearbeitet: Geoff Hayes
am 28 Nov. 2014
When I run the above code, I observe the error
Error using isfield
Too many input arguments.
because the code is trying to pass too many inputs to isfield. You can only check one field per structure at a time. So your above condition would become
if ~isfield(pointpair1,'x') || ~isfield(pointpair1,'y') || ...
~isfield(pointpair2,'x') || ~isfield(pointpair2,'y')
You also need to add a check to make sure that both input parameters are being passed into the function. Use exist to check for their existence. This could replace the nargchkin call which doesn't seem appropriate for this function.
Weitere Antworten (1)
Meet Vora
am 17 Nov. 2021
clc;
clear all;
close all;
point = struct('x',[],'y',[]);
point(1).x = 5;
point(1).y = 3;
point(2).x = 4;
point(2).y = 2;
display(dist3(point));
function distance = dist3(point)
distance = sqrt((point(1).x - point(2).x)^2 + (point(1).y - point(2).y)^2);
end
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!