Filter löschen
Filter löschen

How can I return a value from an excel spreadsheet based on multiple user input variables?

3 Ansichten (letzte 30 Tage)
Hello! I am a beginner in MATLAB and I'm having trouble figuring out the best way of going about writing a particular program.
Basically, I'm trying to write a program that will return the closest matching recipe from an excel spreadsheet based on user input on available ingredients, cost, and time. I'm not sure how to traverse through and filter out the closest match based on several variables.
The spreadsheet is attached for referene.
I want to be able to output a recipe name as well as a link to the closest matching recipe based on the three aforementioned user input variables of ingredients, cost, and time by comparing them to the values on the spreadsheet.
  6 Kommentare
Rik
Rik am 5 Feb. 2024
Typing is very sensitive to errors and is fairly slow. You might consider a list of checkboxes or a uilistbox. That way the user can select the ingredients they have available. You can form the list of ingredients by splitting the lists in your table and using unique. You can then use ismember to check if the available ingredients match a recipe.
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
You should also avoid input without the 's' switch, since it allows executing arbitrary code. Try writing exit as one of the inputs to see what I mean.
Stephen23
Stephen23 am 7 Feb. 2024
In addition to the other comments: rather than assuming exactly equivalent strings (which given real-world data is very unlikely) you should probably check for string edit distance or some other metric of how similar the strings are:
You will also have to consider that the ingredients might be listed in different orders.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Austin M. Weber
Austin M. Weber am 7 Feb. 2024
Here is an example of how you can ask the user to select from a list of ingredients rather type the ingredient names manually:
% Read data from Excel file
data = readtable('recipedataset.xlsx','PreserveVariableNames',true);
% Extract the data from the ingredients column
ingredients = data.ingredients;
% Create separate string array from each row, split the strings by
% commas and then concatenate everything into one variable
all_ingredients = string(); % Preallocate array
for i = 1 : size(ingredients,1)
row2string = string(ingredients(i));
stringSplit = strsplit(row2string,', ')';
all_ingredients = [all_ingredients; stringSplit];
end
% Remove any duplicate ingredients and convert to cell array
unique_ingredients = cellstr(unique(all_ingredients));
% Ask user to select their ingredients from a list
idx = listdlg('ListString',unique_ingredients);
The last function (listdlg) creates a dialoge box where the user can select any number of the ingredients from the table. To select more than one ingredient, the user will have to hold down the CTRL key while selecting ingredients.
Once the ingredients are selected, you can check which recipes in the original table contain the selected ingredients:
% Construct a list of the ingredients chosen by the user
selected_ingredients = unique_ingredients(idx);
% Loop through each recipe to count how many of the chosen ingredients are contained in
% each recipe
recipe_matrix = zeros(length(data.ingredients),length(selected_ingredients));
for j = 1:length(selected_ingredients)
temp = contains(data.ingredients,selected_ingredients);
recipe_matrix(:,j) = temp;
end
% Sum recipe_matrix row-wise to get the total number of matching
% ingredients per recipe
recipe_matches = sum(recipe_matrix,2);
% Identify which recipes' ingredients lists contain the most matches to the
% user selections
max_matches = max(recipe_matches);
most_matches = find(recipe_matches == max_matches);
After this step, the variable most_matches contains the index position of the row(s) in the table data containing the highest number of ingredients from the user selections. To extract these rows, simply use most_matches to index into the table:
% Closest matching recipe(s) based on selected ingredients:
possible_recipes = data(most_matches,:);
If there is more than one possible recipe, you can have the user specify the time and cost and then use those inputs to reduce the recipe list further. I hope this helps!

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by