How do I search these matlab cells/structures?

조회 수: 1 (최근 30일)
Felix
Felix 2012년 7월 5일
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