arrays - Manipulating a struct which is a class property in MATLAB -
i've been trying work matlab classes & structs in order develop traffic simulation. have not worked actively matlab classes before, gets bit tricky @ times. questions involves manipulating struct, property of class.
top-level access
vehicles_handle = vehicleshandle; vehicles_handle.createvehicles(initialtrafficdensity); vehicles_handle.vehicles(1)
class definition
classdef vehicleshandle %vehicleshandle summary of class goes here % detailed explanation goes here properties active_vehicles_count vehicles end methods (access = public) function obj = vehicleshandle obj.active_vehicles_count = 0; obj.vehicles = struct('lane',0,'position',0,'velocity',0); end function obj = createvehicles(obj,initialtrafficdensity) obj.active_vehicles_count = obj.active_vehicles_count + 1; obj.vehicles(1).lane = 1; obj.vehicles(1).position = 3; obj.vehicles(1).velocity = 3; obj.vehicles(2).lane = 2; obj.vehicles(2).position = 3; obj.vehicles(2).velocity = 3; end
now, can't see output expected (which vehicles_handle.vehicles(1)), see them properties of vehicle 1 0's. situation changes, of course, when put in function vehicleshandle, want handle creation of vehicles way.
i know code might not efficient way handle this, want learn handling struct in class without pain. constructive comments , in advance.
getting rid of problem quite easy:
classdef vehicleshandle
has
classdef vehicleshandle < handle
and understand why, please read comparison of handle , value classes.
Comments
Post a Comment