How can I dynamically create and configure a subclass of an abstract class from a file or script in one step in MATLAB?
In MATLAB, I have an abstract class called "Vehicle" and concrete subclasses like "Car" and "Bike". In my "Garage" class, the "vehicle" property is set dynamically at runtime by reading a config script (e.g., "garageConfig.m"). Right now, I use a variable "vehicleType" in "garageConfig.m" to determine which subclass to instantiate. But the problem is I have to:Run "garageConfig.m" to get the value for "vehicleType", then set the property "vehicle" to the correct child (e.g., obj.vehicle = Bike()).Then run "garageConfig.m" again to populate the property values (like "vehicle.color", "vehicle.wheels", etc.) for the instantiated child class.
Is there a better way to do this, so I don’t have to run the config script twice to set up the "vehicle" object and its properties?
% Vehicle.m
classdef (Abstract) Vehicle
properties
color = "white";
wheels = 4;
end
end
% Car.m
classdef Car < Vehicle
end
% Bike.m
classdef Bike < Vehicle
properties
hasBell = false;
end
end
% Garage.m
classdef Garage
properties
vehicle % will be Vehicle
end
methods
function obj = Garage(configFile)
% First run: get vehicleType
run(configFile); % loads vehicleType and vehicle properties
% Instantiate correct subclass
switch vehicleType
case 1
obj.vehicle = Car();
case 2
obj.vehicle = Bike();
end
% Now, to assign properties, need to run the config file again:
run(configFile); % loads vehicle.color, vehicle.wheels, etc.
% Assign properties
obj.vehicle.color = vehicle.color;
obj.vehicle.wheels = vehicle.wheels;
if isa(obj.vehicle, ‘Bike’)
obj.vehicle.hasBell = vehicle.hasBell;
end
end
end
end
% garageConfig.m
vehicleType = 2; % 1 for Car, 2 for Bike
vehicle.color = "red";
vehicle.wheels = 2;
vehicle.hasBell = true;
% Usage:
g = Garage(‘garageConfig.m’);In MATLAB, I have an abstract class called "Vehicle" and concrete subclasses like "Car" and "Bike". In my "Garage" class, the "vehicle" property is set dynamically at runtime by reading a config script (e.g., "garageConfig.m"). Right now, I use a variable "vehicleType" in "garageConfig.m" to determine which subclass to instantiate. But the problem is I have to:Run "garageConfig.m" to get the value for "vehicleType", then set the property "vehicle" to the correct child (e.g., obj.vehicle = Bike()).Then run "garageConfig.m" again to populate the property values (like "vehicle.color", "vehicle.wheels", etc.) for the instantiated child class.
Is there a better way to do this, so I don’t have to run the config script twice to set up the "vehicle" object and its properties?
% Vehicle.m
classdef (Abstract) Vehicle
properties
color = "white";
wheels = 4;
end
end
% Car.m
classdef Car < Vehicle
end
% Bike.m
classdef Bike < Vehicle
properties
hasBell = false;
end
end
% Garage.m
classdef Garage
properties
vehicle % will be Vehicle
end
methods
function obj = Garage(configFile)
% First run: get vehicleType
run(configFile); % loads vehicleType and vehicle properties
% Instantiate correct subclass
switch vehicleType
case 1
obj.vehicle = Car();
case 2
obj.vehicle = Bike();
end
% Now, to assign properties, need to run the config file again:
run(configFile); % loads vehicle.color, vehicle.wheels, etc.
% Assign properties
obj.vehicle.color = vehicle.color;
obj.vehicle.wheels = vehicle.wheels;
if isa(obj.vehicle, ‘Bike’)
obj.vehicle.hasBell = vehicle.hasBell;
end
end
end
end
% garageConfig.m
vehicleType = 2; % 1 for Car, 2 for Bike
vehicle.color = "red";
vehicle.wheels = 2;
vehicle.hasBell = true;
% Usage:
g = Garage(‘garageConfig.m’); In MATLAB, I have an abstract class called "Vehicle" and concrete subclasses like "Car" and "Bike". In my "Garage" class, the "vehicle" property is set dynamically at runtime by reading a config script (e.g., "garageConfig.m"). Right now, I use a variable "vehicleType" in "garageConfig.m" to determine which subclass to instantiate. But the problem is I have to:Run "garageConfig.m" to get the value for "vehicleType", then set the property "vehicle" to the correct child (e.g., obj.vehicle = Bike()).Then run "garageConfig.m" again to populate the property values (like "vehicle.color", "vehicle.wheels", etc.) for the instantiated child class.
Is there a better way to do this, so I don’t have to run the config script twice to set up the "vehicle" object and its properties?
% Vehicle.m
classdef (Abstract) Vehicle
properties
color = "white";
wheels = 4;
end
end
% Car.m
classdef Car < Vehicle
end
% Bike.m
classdef Bike < Vehicle
properties
hasBell = false;
end
end
% Garage.m
classdef Garage
properties
vehicle % will be Vehicle
end
methods
function obj = Garage(configFile)
% First run: get vehicleType
run(configFile); % loads vehicleType and vehicle properties
% Instantiate correct subclass
switch vehicleType
case 1
obj.vehicle = Car();
case 2
obj.vehicle = Bike();
end
% Now, to assign properties, need to run the config file again:
run(configFile); % loads vehicle.color, vehicle.wheels, etc.
% Assign properties
obj.vehicle.color = vehicle.color;
obj.vehicle.wheels = vehicle.wheels;
if isa(obj.vehicle, ‘Bike’)
obj.vehicle.hasBell = vehicle.hasBell;
end
end
end
end
% garageConfig.m
vehicleType = 2; % 1 for Car, 2 for Bike
vehicle.color = "red";
vehicle.wheels = 2;
vehicle.hasBell = true;
% Usage:
g = Garage(‘garageConfig.m’); abstractclass, subclass, classconfig MATLAB Answers — New Questions









