Why aren't the outcomes of class methods being applied to global variables:

1 vue (au cours des 30 derniers jours)
Alexander James
Alexander James le 2 Mai 2020
I'm not sure why the outcomes of class methods are not being applied to global variables:
When I enter
clear(Forest_Road,shoot)
in the main script I do not receive any errors but I don't have any trees being removed from my "forest" here grouped as collection of shoot's in a non scaler array, I think this is by design (seperating class varaibles from global variables). I tried making the input argument a global variable in the class definition but it didn't affect the outcome, what am I missing?
classdef Road
% Creation of the class road
properties
x;
y;
c;
end
methods
% Constructer
function obj = Road(x,y,c)
% Construct an instance of road class
% Assign input properties
obj.x = x;
obj.y = y;
obj.c = c;
end
function chop_trees = clear(obj,shoot)
i=1;
j=1;
while j~=80
for j=1:80
dist = hypot([shoot(i).x] - obj.x(j), [shoot(i).y] - [obj.y(j) + obj.c]);
if any(dist < 2);
global shoot
shoot(i)=[];
end
end
i=i+1;
end
end
end
end

Réponses (1)

Steven Lord
Steven Lord le 2 Mai 2020
Your class is a value class rather than a handle class. See the description of the difference between those two types of object here.
  2 commentaires
Alexander James
Alexander James le 3 Mai 2020
Thank you fot the clarification, If I set my superclass Geometry < handle then my subclass Road should inherit those properties. However if type
clear(Forest_Road,Forest)
in my main script the number and execute the number of objects (trees) in my Forest still not decrease, I've checked the values calculated under dist and there objects (trees) closer than 2m
classdef Road < Geometry
% Creation of the class road
properties
c;
end
methods
% Constructer
function obj = Road(x,y,c)
% Construct an instance of road class
% Assign input properties
obj@Geometry(x,y);
obj.x = x;
obj.y = y;
obj.c = c;
end
function clear_trees = clear(obj,Forest)
i=1;
j=1;
global Forest
while j~=80
for j=1:80
dist = hypot([Forest(i).x] - obj.x(j), [Forest(i).y] - [obj.y(j) + obj.c])
if any(dist < 2);
Forest(i)=[];
end
end
i=i+1;
end
end
end
end
Alexander James
Alexander James le 3 Mai 2020
I tried just setting the road < handle and that didn't work either

Connectez-vous pour commenter.

Catégories

En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by