How do I search these matlab cells/structures?

1 vue (au cours des 30 derniers jours)
Felix
Felix le 5 Juil 2012
I saved my experimental data in this mixture of cells and structures, but now I'm having a hard time searching in it.
first there is a cell containing animal{1,1} to animal{1,6} each contains some information strings and another cell animal{1,n}.traces{1,m} where again structures with more information and several measurements (traces) are stored for each measurement there are three regions animal{1,n}.traces{1,m}.region{1,p} the actual data points are eventually under animal{1,n}.traces{1,m}.region{1,p}.data
for clarity, please see the mat file which I uploaded here https://www.dropbox.com/sh/sn6oqsngkrby3cl/zJSqMvUVzt/data.mat
What I want to do is now to search for example for all n and m indices, which have animal{1,n}.traces{1,m}.stimulus == '1mA' and/or animal{1,n}.traces{1,m}.anesthesia == 'isofluran'

Réponse acceptée

Ken Atwell
Ken Atwell le 5 Juil 2012
Interesting problem. Since the length of traces is not consistent, and their subfields may not exist, some degree of error checking is needed. Here is a brute-force way to do this:
for n=1:numel(animal)
for m=1:numel(animal{n}.traces)
if ( isfield(animal{n}.traces{m}, 'stimulus') && ...
strcmp(animal{n}.traces{m}.stimulus, '1mA') ) || ...
( isfield(animal{n}.traces{m}, 'anesthesia') && ...
strcmp(animal{n}.traces{m}.anesthesia, 'isofluran' ) )
fprintf('animal{%d}.traces{%d} meets criteria\n', n, m);
end
end
end
Here is a second way that is arguably more elegant, but the unmitigated use of try/catch will mask other potential problems in the data:
maxM = max(cellfun(@(x) numel(x.traces), animal));
[N,M]=meshgrid(1:numel(animal),1:maxM);
M=M(:); N=N(:);
for i=1:numel(M)
try
if strcmp(animal{N(i)}.traces{M(i)}.stimulus, '1mA') || ...
strcmp(animal{N(i)}.traces{M(i)}.anesthesia, 'isofluran' )
fprintf('animal{%d}.traces{%d} meets criteria\n', N(i), M(i));
end
catch %#ok<CTCH>
end
end

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by