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.
9 次查看(过去 30 天)
显示 更早的评论
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.
回答(1 个)
Ishan
2022-11-30
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 个评论
Jan
2022-11-30
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!