# User input compared with excel sheet data and returns best fit

1 view (last 30 days)
ML on 10 May 2022
Commented: Mathieu NOE on 11 May 2022
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:
Not sure if this makes snese so I will be happy to clarify.

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_values = cell2num(data(2:end,2:end));
%% step 1 : select which criteria to use
answer = questdlg('select which criteria to use', ...
'miles per gas','horsepower','top speed','top speed');
% Handle response
case 'miles per gas'
clc
col = 1;
case 'horsepower'
clc
col = 2;
case 'top speed'
clc
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
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_values = mycell2mat(data(2:end,2:end));
%% step 1 : select which criteria to use
answer = questdlg('select which criteria to use', ...
'miles per gas','horsepower','top speed','top speed');
% Handle response
case 'miles per gas'
clc
col = 1;
case 'horsepower'
clc
col = 2;
case 'top speed'
clc
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