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.
2 ビュー (過去 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 件のコメント
回答 (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!