필터 지우기
필터 지우기

Find a value in structure

조회 수: 161 (최근 30일)
AI-CHI Chang
AI-CHI Chang 2022년 3월 22일
편집: Vatsal 2023년 10월 7일
The answer below that question is.
valuetofind = 58;
find(arrayfun(@(s) ismember(valuetofind, s.cluster), clusters))
But if I want to find one value in different fields?
e.g. 18841 maybe in e1||e2||e3 ,and I want to return the index 4
  댓글 수: 3
Vatsal
Vatsal 2023년 9월 29일
편집: Vatsal 2023년 10월 7일
I understand that you want to find a value in different fields of a structure and if the value exists, the index should be returned. As Arif Hoq mentioned, you can do that with the “ismember” function. I am attaching the code below to find a value in the different fields of a structure and to return the index of the value:
yourStruct = struct('e1',{1 2 3 18841},'e2',{1 2 18841 4},'e3',{1 2 3 4});
valueToFind = 18841;
fieldsToSearch = {'e1', 'e2', 'e3'}; % Specify the fields to search
index = find(arrayfun(@(s) any(ismember(valueToFind, s.(fieldsToSearch{1}))) || ...
any(ismember(valueToFind, s.(fieldsToSearch{2}))) || ...
any(ismember(valueToFind, s.(fieldsToSearch{3}))), yourStruct), 1);
You can also refer to the MATLAB documentation for the functions used in the above code to obtain more information on its usage and syntax. The links are provided below: -
I hope this helps!
Voss
Voss 2023년 10월 7일
@Vatsal: You can't dynamically reference multiple fields of a struct using a cell array of field names:
yourStruct = struct('e1',{1 2 3 18841},'e2',{1 2 18841 4},'e3',{1 2 3 4})
yourStruct = 1×4 struct array with fields:
e1 e2 e3
valueToFind = 18841;
fieldsToSearch = {'e1', 'e2', 'e3'}; % Specify the fields to search
index = find(arrayfun(@(s) any(ismember(valueToFind, s.(fieldsToSearch))), yourStruct), 1);
Argument to dynamic structure reference must evaluate to a valid field name.

Error in solution>@(s)any(ismember(valueToFind,s.(fieldsToSearch))) (line 4)
index = find(arrayfun(@(s) any(ismember(valueToFind, s.(fieldsToSearch))), yourStruct), 1);

댓글을 달려면 로그인하십시오.

답변 (1개)

Voss
Voss 2023년 10월 7일
yourStruct = struct('e1',{1 2 3 18841},'e2',{1 2 18841 4},'e3',{1 2 3 4})
yourStruct = 1×4 struct array with fields:
e1 e2 e3
valueToFind = 18841;
fieldsToSearch = {'e1', 'e2', 'e3'}; % Specify the fields to search
index = cellfun(@(f) find([yourStruct.(f)] == valueToFind, 1), fieldsToSearch, 'UniformOutput', false)
index = 1×3 cell array
{[4]} {[3]} {1×0 double}
Here index gives you the index of the element of yourStruct that contains the first instance of valueToFind in each field in fieldsToSearch. E.g., in this case index tells you that 18841 appears as yourStruct(4).e1, yourStruct(3).e2, and doesn't appear in [yourStruct.e3] at all.
yourStruct(4).e1
ans = 18841
yourStruct(3).e2
ans = 18841
[yourStruct.e3]
ans = 1×4
1 2 3 4
You can do further processing on index to, say, get the index of the first instance of valueToFind in any searched field of yourStruct, and which field it appeared in:
index(~cellfun(@isscalar,index)) = {Inf}
index = 1×3 cell array
{[4]} {[3]} {[Inf]}
[min_index,field_index] = min([index{:}])
min_index = 3
field_index = 2
found_field = fieldsToSearch{field_index}
found_field = 'e2'

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by