matlab - Find a value in a field of a cell array of structures -


having defined gnu/octave cell array

dict =  {   [1,1] =     scalar structure containing fields:       id = integers       vl = ... -2, -1, 0, 1, 2, ...   [1,2] =     scalar structure containing fields:       id = positive integers       vl = 1, 2, 3, 4, ...   [1,3] =     scalar structure containing fields:       id = negative integers       vl = -1, -2, -3, -4, ... } 

how find (in octave code, without looping!) structs have given value in field, say, containing 'integer' in id field, or -1 in vl field.

update: command operate find(dict,'vl','-1'), returning 1, 3, or 1 0 1.

a possible implementation of search function like:

function arrayvalues = findinfield(mycellarray, inputfield, outputfield, fieldvalue)    positions = ~cellfun(@isempty, regexp({[mycellarray{:}].(inputfield)}, fieldvalue));   arrayvalues = {[mycellarray{positions}].(outputfield)};  end 

for exact matching add ^ , $ beginning , end, respectively, of regular expression.

use cases:

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, ...} } 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -