如何提取名称的一部分提取结构元素名称



我当前正在处理许多使用长仿真(3DAYS (创建的许多.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*');的线路做某事的方法?

假设您有类似的东西:

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 String的字段

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是否在开始时。您还应该检查功能结束时是否发现了一个字段。

最新更新