Find a value in structure

164 次查看(过去 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