How would i use for loop to display the whole row of information based on userinput of the first collum. The data comes from an excel sheet.

1 view (last 30 days)
Find a recipe user input + Error Check
F = input(['Great! You chose to (Find A Recipe) now, please enter a material ', ...
'or item that you would like to see the recipe off! '], 's');
while ~strcmpi(F,All_data)
F = input('Sorry invalid choice, please try again! ','s');
end
for k = 1:1:length(F)
if F == All_data(k,1)
final_recipe = All_data(k,:);
fprintf('this is %f\n ',final_recipe)
end
end
Is just not working.

Answers (1)

Ishan
Ishan on 30 Nov 2022
Hi Allen,
If you want to loop over the entire row, it would be better to use indexing to extract the row number and display all elements of that particular row using All_data(idx,:)
You can search for the index (idx) using the find function along with strcmp to get that row number.
Here are links to a few concepts that would be useful for performing such operations:
1. Matrix Indexing: Using Logical in Array Indexing
2. Find array elements based on a specified condition
3.Strcmp function
Alternatively, if you want to get the row with the DisplayName ‘XYZ’ based on the name in a given table (where DisplayName is the heading of that table column and XYZ is 1st element of the row to be extracted, you can use
>> data(data.DisplayName == 'XYZ', :)
  1 Comment
Jan
Jan on 30 Nov 2022
data(data.DisplayName == 'XYZ', :)
Comparing CHAR vectors with == is fragile, when the number of elements is not equal. Set at least the 2nd argument in double quotes to use strings.

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!