Matlab 在结构单元数组的字段中查找值
定义了GNU/倍频程单元阵列Matlab 在结构单元数组的字段中查找值,matlab,octave,cell-array,Matlab,Octave,Cell Array,定义了GNU/倍频程单元阵列 dict = { [1,1] = scalar structure containing the fields: id = integers vl = ... -2, -1, 0, 1, 2, ... [1,2] = scalar structure containing the fields: id = positive integers vl = 1, 2, 3, 4, ... [1,
dict =
{
[1,1] =
scalar structure containing the fields:
id = integers
vl = ... -2, -1, 0, 1, 2, ...
[1,2] =
scalar structure containing the fields:
id = positive integers
vl = 1, 2, 3, 4, ...
[1,3] =
scalar structure containing the fields:
id = negative integers
vl = -1, -2, -3, -4, ...
}
如何查找(在倍频程代码中,不循环!)字段中具有给定值的结构,例如,在id
字段中包含'integer'
,或在vl
字段中包含-1
更新:该命令的操作方式类似于
find(dict、'vl'、'-1')
,返回1、3
、或1 0 1
,搜索功能的可能实现方式如下所示:
function arrayvalues = findinfield(mycellarray, inputfield, outputfield, fieldvalue)
positions = ~cellfun(@isempty, regexp({[mycellarray{:}].(inputfield)}, fieldvalue));
arrayvalues = {[mycellarray{positions}].(outputfield)};
end
要获得精确匹配,请分别在正则表达式的开头和结尾添加^
和$
用例:
findinfield(dict, "id", "vl", "integers")
ans =
{
[1,1] = ... -2, -1, 0, 1, 2, ...}
[1,2] = 1, 2, 3, 4, ...}
[1,3] = -1, -2, -3, -4, ...}
}
findinfield(dict, "id", "vl", "^integers$")
ans =
{
[1,1] = 1, 2, 3, 4, ...}
}
findinfield(dict, "id", "vl", "negative")
ans =
{
[1,1] = -1, -2, -3, -4, ...}
}
可以使用动态字段引用,其中字段存储在作为参数传递的字符串
fieldName
中:arrayvalues={[mycellarray{positions}](fieldName)}代码>不错,@gnovice!!