Need the function to run multiple times and store each data point for each input variable

4 次查看(过去 30 天)
The fucntion calculates the time spent on an excursion for a customer, but now that i have twenty customers. how can i call the function to calculate the time for each customer and store the data in my work space?
function time = getTimePerTrip(plane, customer, altitude)
Fleet = plane;
if customer.Weight > Fleet.WeightCapacity
warning('Weight exceeds plane capacity');
end
if customer.Experience == "Beginner"
t_spent = 30;
elseif customer.Experience == "Experienced"
t_spent = 10;
end
time_ascend = altitude/Fleet.LiftRate; % time taken to reach altitude
time_decend = 5*time_ascend; % time taken to decend to the ground
time = t_spent + time_decend + time_ascend; % total time
end
  5 个评论
Pratik Chachad
Pratik Chachad 2021-8-10
If my previous assumption is true, Something like this should work.
function time_array = getTimePerTrip(plane, customer, altitude)
Fleet = plane;
% This will be the 20 customers
all_customers = fieldnames(customer);
%Store the time results in an array with same size as no of customers
time_array = zeros(length(all_customers),1);
for i=1:length(all_customers)
current_customer = customer.(all_customers{i});
if current_customer.Weight > Fleet.WeightCapacity
warning('Weight exceeds plane capacity');
end
if current_customer.Experience == "Beginner"
t_spent = 30;
elseif current_customer.Experience == "Experienced"
t_spent = 10;
end
time_ascend = altitude/Fleet.LiftRate; % time taken to reach altitude
time_decend = 5*time_ascend; % time taken to decend to the ground
time_array(i) = t_spent + time_decend + time_ascend; % total time
end
end
Tebogo Makobe
Tebogo Makobe 2021-8-10
@Pratik Chachad The customer data is a structure (1 x 20), with Fleet also as a structure (5 x 1). The function is supposed to be able to take mulitple customers and culculate the time spent for the excursion, and store time for each customer for further analysis.

请先登录,再进行评论。

回答(1 个)

Prahlad Gowtham Katte
Hello,
I understood that you want to execute the same function for customer where it is a 1x20 struct array. The following function is a modified function which calculates time per customer and stores it as an array.
function alltime = getTimePerTrip(plane, customer, altitude)
Fleet = plane;
alltime=zeros(1,length(customer));
for i=1:length(customer)
current_customer=customer(i);
if current_customer.Weight > Fleet.WeightCapacity
warning('Weight exceeds plane capacity');
end
if current_customer.Experience == "Beginner"
t_spent = 30;
elseif current_customer.Experience == "Experienced"
t_spent = 10;
end
time_ascend = altitude/Fleet.LiftRate; % time taken to reach altitude
time_decend = 5*time_ascend; % time taken to decend to the ground
time = t_spent + time_decend + time_ascend; % total time
alltime(i)=time;
end
end
The following links can be helpful in learning more about structures.
Hope it helps

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by