필터 지우기
필터 지우기

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 (최근 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
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
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 CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by