本文介绍了如何在MATLAB中扫描几种结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些具有相似字段名称的结构数组(例如structure1,structure2,structure3等).我想浏览所有结构并仅返回第一个字段为5(Field1 == 5)的那些结构.到目前为止,我已经有了这段代码,

I have some structure arrays (like structure1, structure2, structure3,...) with similar field names. I want to scan through all the structures and return only those whose first field is 5 (Field1==5). I have this code so far,

for k=1:3
    s=sprintf('Structure%d',k)
    Structure=load(s)
    idx=cellfun(@(x) x==5, {Structure.Field1})
    out=Structure(idx)
    v{k}={Structure.Field1}
end

但是它给了我这个错误:

but it gives me this error:

Reference to non-existent field 'Field1'. 

有人可以指出这里有什么问题吗?

Can someone please point out whats wrong here?

感谢

推荐答案

for k=1:3
    s=sprintf('Structure%d',k)
    Structure=load(s)
    eval(['newStructure(k)=Structure.' s]);
    idx(k)=cellfun(@(x) x==5, {newStructure(k).Field1})
end
%extract the structures from newStructure which have 1 in idx
out=newStructure(idx); %idx should be a logical array
for i=1:size(out,2)
    v(i)=out(i).Field1;
end

这应该工作完美.

这篇关于如何在MATLAB中扫描几种结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 09:57