How do I search these matlab cells/structures?

1 次查看(过去 30 天)
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'

采纳的回答

Ken Atwell
Ken Atwell 2012-7-5
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by