structfind

版本 1.1.0.0 (1.9 KB) 作者: Dirk-Jan Kroon
Search for a value, cell or string inside a Matlab struct
3.7K 次下载
更新时间 2010/12/23

查看许可证

StructFind, Find the index of a certain string or value in a struct

index=structfind(a,field,value)

inputs,
a : A Matlab struct, for example a(1).name='red', a(2).name='blue';
field : The name of the field which is searched, for example 'name'
value : The search value, for example 'blue'

outputs,
index : The Struct index which match the search

Example,

a(1).name='blue';
a(1).index=0;
a(1).val='g';

a(2).name='red';
a(2).index=1;
a(2).val=[1 0];

a(3).name='green';
a(3).index=2;
a(3).val='g';

a(4).name='black';
a(4).index=3;
a(4).val=[0 0 0];

a(5).name='yellow';
a(5).index=NaN;
a(5).val=[0 1 1];

a(6).name='orange';
a(6).index=[];
a(6).val=[1 1 0];

a(7).name='brown';
a(7).index=6;
a(7).val={'12'};

a(8).name='white';
a(8).index=7;
a(8).val.x=1;

a(8).name='purple';
a(8).index=8;
a(8).val.child.value=2;

index=structfind(a,'name','red');
disp(['index : ' num2str(index)])

index=structfind(a,'index',1);
disp(['index : ' num2str(index)])

index=structfind(a,'val',[0 0 0]);
disp(['index : ' num2str(index)])

index=structfind(a,'val','g');
disp(['index : ' num2str(index)])

index=structfind(a,'index',NaN);
disp(['index : ' num2str(index)])

index=structfind(a,'index',[]);
disp(['index : ' num2str(index)])

index=structfind(a,'val',{'12'});
disp(['index : ' num2str(index)])

index=structfind(a,'val.child.value',2);
disp(['index : ' num2str(index)])

引用格式

Dirk-Jan Kroon (2024). structfind (https://www.mathworks.com/matlabcentral/fileexchange/29808-structfind), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2010b
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.1.0.0

Added solutions for problem cases by comment : Jos (10584)

1.0.0.0