2017-04-24 131 views
0

我目前后处理了大量的具有长的仿真(3天+)创建.MAT文件(300+)。如何提取给定名称的一部分结构元素的名称?

每个垫子文件包含几个变量,他们的每一个网格上(是的,他们已经与eval创建)的位置而得名。

我由此产生打开每个文件的顺序

s = what(pwd); 
s = s.mat; 

for i=1:length(s) 
    data = open(sprintf('%s',s{i,:})); 
    % here goes what I would like to do 
end 

什么我想现在要做的就是以提取符合某个特定模式的数据组件的当前名称的脚本。

具体来说,我知道在data有一个名为coef_%i_%i的载体,我想分离它并将其分配给多维数组。

第二部分我知道如何去做,我可以扫描_字符的变量名,隔离整数并将向量分配到它在数组中的适当位置。

我的问题是:

  • 在Matlab是有办法做到沿vectorname = extractname('data.coef*');线的东西?

回答

3

假设你有这样的:

clear data 
% create a dummy field 
name = sprintf ('coef_%i_%i', randi(100,[2 1])); 
% a data struct with a field which starts "coef*" 
data.(name) = rand; 
% and your data field may contain some other fields? 
data.other = []; 

然后,您可以提取出其中包含的COEF串

function output = extractname (data) 
    %extract the fieldnames from data: 
    fn = fieldnames(data); 

    % find all that have coef in them 
    index = cellfun(@isempty, strfind(fn,'coef')); 
    % remove any that dont have coef in them 
    fn(index) = []; 

    %You are then left with one(or more) that contain coef in the name: 
    output = fn; 
end 

领域如果你的数据结构包含字段可能有“COEF”在名字的其他地方,你需要通过它们来检查coef是否在开始。您也应该检查,在有且只有一个领域已经发现你的函数结束。

+0

'指数= cellfun(@(X)的isEmpty(x)的|| X> 1,正则表达式(FN, 'COEF'))'应该只匹配,与'coef'开头的名称。 – nekomatic

+0

我是浑然不觉的'字段名存在()',谢谢! – Federico

相关问题