Creating an array of object with variable inputs

1 view (last 30 days)
I have the following codes
Number_of_lanes = input('Enter the total number of lanes: ');
Prediction_Horizon = input('Enter the prediction horizon in sec: '); % In sec
time_step = 1; % In sec
Total_number_of_cars = input('Enter the total number of cars: ');
Number_of_cars_in_lane = zeros(Number_of_lanes,1);
Sum_of_cars = 0;
Velocity_Min = ((20)/(3.6)); % In m/sec
Velocity_Max = ((80)/(3.6)); % In m/sec
acceleration_Min = -8; % In m/sec^2
acceleration_Max = 8; % In m/sec^2
s0 = 2; % Safety distance in m
desired_velocity = ((80)/(3.6)); % In m/sec
width_of_lane = 3.5; % In m
time_headway = 2; % In sec
%Assigning number of cars in each lane
for i = 1:Number_of_lanes
Number_of_cars_in_lane(i) = input(['Number of cars in lane ' num2str(i) ' : ']);
Sum_of_cars = sum(Number_of_cars_in_lane);
end
%Error Handling
if Sum_of_cars ~= Total_number_of_cars
error('Number of cars in lane must be equal to total number of cars');
end
% Empty template of each car
empty_car.velocity = []; % velocity of car
empty_car.acceleration = []; % acceleration of car
empty_car.lane =[]; % lane of car
empty_car.s_required = []; % spacing required in front of car
empty_car.s_actual = []; % actual spacing in front of car
empty_car.position = []; % postion of car in lane
empty_car.distance_travelled = []; % distance travelled by car in last second
empty_car.velocity_min = Velocity_Min; % mimimum allowable velocity
empty_car.velocity_max = Velocity_Max; % maximum allowable velocity
empty_car.acceleration_min = acceleration_Min; % minimum allowable acceleration
empty_car.acceleration_max = acceleration_Max; % maximum allowable acceleration
empty_car.Objective = []; % objective function value
% Creating cars with parameters
Cars = repmat(empty_car, Total_number_of_cars, Number_of_lanes, Prediction_Horizon);
How can I convert this into an array of object class.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!