matlab - selecting specific cells in a structure array based on the values of the field -


data looks more this:

data = struct('direction',{[1,1,1,1],[1,1,2,1],[2,2,2,2,2],[2,2,2,2,1,2],[2,2,2,2]},'tr‌ial'{'correct','incorrect','incorrect','correct','correct'});  

this example , have other fields well

so example, want work cells in struct have trial correct, want select cells , store cells in separate struct. not sure if i'm clear or not apologize that.

same if want select cells direction field vector have here different sizes want select vectors elements "2" only.

thank you

you can filter elements trial = 'correct' this:

data = data(arrayfun(@(x) strcmp(x.trial, 'correct'), data)) 

if want filter elements direction = 2 (all values), this:

data = data(arrayfun(@(x) all(x.direction == 2), data)) 

or can above in 1 line this:

data = data(arrayfun(@(x) strcmp(x.trial, 'correct') & all(x.direction == 2), data)) 

Comments

Popular posts from this blog

angularjs - Redirect to a new template in angular js -

Oracle SQL. How to select specific IDs where value of columns specify criteria? -

Desktop Launcher for Python Script Starts Program in Wrong Path (Linux) -