Clear Filters
Clear Filters

Select data based on user input

2 views (last 30 days)
Kaden
Kaden on 30 Apr 2023
Commented: albara on 30 Apr 2023
I am required to write a code that uses a function to calulate drive time based on user input of which car manufactor they choose, and then get the mean drive time for the amount of cars. I have imported the excel sheet with the four different car manufactors but i can not figure how to get the function to only use one manufactor based on the input. Any help would be greatly appreciated.
manufacturer = input('Enter manufacturer name (Toyota, Ford, BMW, Kia): ','s');
drive_time = drivetime(MilesPerGallon,TankSizeGallons,AverageSpeedMilesPerHour);
mean_drive_time = mean(drive_time);
fprintf('The mean drive time for %s is %.2f hours.\n', manufacturer, mean_drive_time);
and the function is
function [DriveTime] = drivetime(MilesPerGallon,TankSizeGallons,AverageSpeedMilesPerHour)
% drivetime
% This function will calculate the drive time for the manufactors vehicles
DriveTime = (MilesPerGallon*TankSizeGallons)/AverageSpeedMilesPerHour;
end

Answers (1)

albara
albara on 30 Apr 2023
To accomplish this task, you need to filter the data by the chosen manufacturer and then pass the corresponding data to the drivetime function. You can use logical indexing to filter the data based on the manufacturer name. Here's a modified version of your code:
% Import data from the Excel sheet
data = readtable('your_excel_file.xlsx');
% Get user input
manufacturer = input('Enter manufacturer name (Toyota, Ford, BMW, Kia): ', 's');
% Filter data by manufacturer
filtered_data = data(strcmp(data.Manufacturer, manufacturer), :);
% Calculate drive time for each car
drive_time = drivetime(filtered_data.MilesPerGallon, filtered_data.TankSizeGallons, filtered_data.AverageSpeedMilesPerHour);
% Calculate mean drive time
mean_drive_time = mean(drive_time);
% Display result
fprintf('The mean drive time for %s is %.2f hours.\n', manufacturer, mean_drive_time);
And the drivetime function remains the same:
function [DriveTime] = drivetime(MilesPerGallon,TankSizeGallons,AverageSpeedMilesPerHour)
% drivetime
% This function will calculate the drive time for the manufactors vehicles
DriveTime = (MilesPerGallon*TankSizeGallons)/AverageSpeedMilesPerHour;
end
Make sure to replace 'your_excel_file.xlsx' with the name of your actual Excel file. This code assumes that the Excel sheet has columns named 'Manufacturer', 'MilesPerGallon', 'TankSizeGallons', and 'AverageSpeedMilesPerHour'. Adjust the column names in the code if they are different in your sheet.
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes and you might find better answer
Kindly give me your feedback
  2 Comments
Kaden
Kaden on 30 Apr 2023
okay thank you, but now its giving me an error with the readtable function.
albara
albara on 30 Apr 2023
It seems like you might not have the required toolbox to use the readtable function or there could be a problem with the file path or format. Here's an alternative approach using xlsread function which should work without any additional toolbox:
% Import data from the Excel sheet
[num, txt, raw] = xlsread('your_excel_file.xlsx');
% Get user input
manufacturer = input('Enter manufacturer name (Toyota, Ford, BMW, Kia): ', 's');
% Filter data by manufacturer
manufacturer_col = strcmp(txt(1,:), 'Manufacturer');
manufacturer_data = txt(2:end, manufacturer_col);
rows_to_keep = strcmp(manufacturer_data, manufacturer);
% Get the corresponding data for MilesPerGallon, TankSizeGallons, and AverageSpeedMilesPerHour
MilesPerGallon_col = strcmp(txt(1,:), 'MilesPerGallon');
TankSizeGallons_col = strcmp(txt(1,:), 'TankSizeGallons');
AverageSpeedMilesPerHour_col = strcmp(txt(1,:), 'AverageSpeedMilesPerHour');
MilesPerGallon_data = num(rows_to_keep, MilesPerGallon_col - 1);
TankSizeGallons_data = num(rows_to_keep, TankSizeGallons_col - 1);
AverageSpeedMilesPerHour_data = num(rows_to_keep, AverageSpeedMilesPerHour_col - 1);
% Calculate drive time for each car
drive_time = drivetime(MilesPerGallon_data, TankSizeGallons_data, AverageSpeedMilesPerHour_data);
% Calculate mean drive time
mean_drive_time = mean(drive_time);
% Display result
fprintf('The mean drive time for %s is %.2f hours.\n', manufacturer, mean_drive_time);
And the drivetime function remains the same:
function [DriveTime] = drivetime(MilesPerGallon,TankSizeGallons,AverageSpeedMilesPerHour)
% drivetime
% This function will calculate the drive time for the manufactors vehicles
DriveTime = (MilesPerGallon*TankSizeGallons)/AverageSpeedMilesPerHour;
end
Again, make sure to replace 'your_excel_file.xlsx' with the name of your actual Excel file. This code assumes that the Excel sheet has columns named 'Manufacturer', 'MilesPerGallon', 'TankSizeGallons', and 'AverageSpeedMilesPerHour'. Adjust the column names in the code if they are different in your sheet.
Note that the xlsread function works for .xls files (Excel 97-2003 format) and might work for .xlsx files depending on your MATLAB version. If you still face issues, please let me know the error message and your MATLAB version, and I'll help you find an alternative solution.
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes and you might find better answer

Sign in to comment.

Categories

Find more on Data Import from MATLAB 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!