How to find a structure point with two fields x and y and then find the distance between two points
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (1)
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
1 Commento
Deven Kyser
il 5 Apr 2022
The real GOAT
Categorie
Scopri di più su Argument Definitions in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!