User input compared with excel sheet data and returns best fit

1 view (last 30 days)
I want to create a program that for examplle reads an excel data sheet with x, y, and z car and each have different parameters (miles per gas, horsepower, top speed etc )... I want to have user input be compared to each one to give me the best car option according to input parameters.LKike maybe have 5% difference ffrom the data and input,,,,,
data:
x car has 50miles per gas, 200horsepower, 150mph
y car has 40miles per gas, 350horsepower, 100mph
z car has 30miles per gas, 120horsepower, 250mph
User input:
user car has 45 miles 300horsepower, 120mph
Output:
Y is your best fit
Not sure if this makes snese so I will be happy to clarify.

Accepted Answer

Mathieu NOE
Mathieu NOE on 10 May 2022
hello
was a nice recreation task.... here a small demo (the excel file is attached)
% Import the data
data = readcell("CARS.xlsx");
data_values = cell2num(data(2:end,2:end));
%% step 1 : select which criteria to use
answer = questdlg('select which criteria to use', ...
'Menu', ...
'miles per gas','horsepower','top speed','top speed');
% Handle response
switch answer
case 'miles per gas'
clc
disp([answer ' selected'])
col = 1;
case 'horsepower'
clc
disp([answer ' selected'])
col = 2;
case 'top speed'
clc
disp([answer ' selected'])
col = 3;
end
%% step 2 : search nearest car according to criteria and input value (below) :
prompt = "What is the target value? ";
x = input(prompt);
data_values_col = data_values(:,col);
pc = data_values_col./x;
ind = find(pc>=0.95 & pc<=1.05); % +/- 5% tolerance
% found car closest to query value :
if ~isempty(ind)
out = [data(1,:) ; data(1+ind,:)]
else
disp('No solution found within 5% tolerance');
end
  5 Comments
Mathieu NOE
Mathieu NOE on 11 May 2022
hello @RV123
It works with both cell2num and cell2mat on my R2020b
which release are you using ?
Mathieu NOE
Mathieu NOE on 11 May 2022
if you still have trouble with cell2mat , here a simple homde made alternative in the code (see function at the bottom end)
% Import the data
data = readcell("CARS.xlsx");
data_values = mycell2mat(data(2:end,2:end));
%% step 1 : select which criteria to use
answer = questdlg('select which criteria to use', ...
'Menu', ...
'miles per gas','horsepower','top speed','top speed');
% Handle response
switch answer
case 'miles per gas'
clc
disp([answer ' selected'])
col = 1;
case 'horsepower'
clc
disp([answer ' selected'])
col = 2;
case 'top speed'
clc
disp([answer ' selected'])
col = 3;
end
%% step 2 : search nearest car according to criteria and input value (below) :
prompt = "What is the target value? ";
x = input(prompt);
data_values_col = data_values(:,col);
pc = data_values_col./x;
ind = find(pc>=0.95 & pc<=1.05); % +/- 5% tolerance
% found car closest to query value :
if ~isempty(ind)
out = [data(1,:) ; data(1+ind,:)]
else
disp('No solution found within 5% tolerance');
end
%%%%%%% functions %%%%%%%
function m = mycell2mat(c)
% only for cell array 2-D
if length(size(c)) == 2
rows = size(c,1);
m = [];
% Concatenate each row first
for n=1:rows
m = [m; c{n,:}];
end
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Linearization 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!