【问题标题】:Dynamically calling gather function in MatLabMatLab中动态调用gather函数
【发布时间】:2021-12-13 20:45:46
【问题描述】:

我正在尝试在 MatLab 中动态调用 gather() 函数。

我目前正在做这样的事情:

for index_1 = 1:1:document_count
  current_filename = "random_file"+index_1+".mat";
  data = load(current_filename);
  pci= gather(datasets.current_filename.pci.dist);
end 

在上面,数据已加载,但收集函数失败,我假设是因为我在中间传递了一个字符串。我不确定有什么解决方法。

任何建议将不胜感激。

【问题讨论】:

  • this 有帮助吗?

标签: matlab gather


【解决方案1】:

将字符串 (current_filename) 用作struct 的字段名时,需要将其放在括号中。见here

for index_1 = 1:1:document_count
   current_filename = "random_file"+index_1+".mat";
   data = load(current_filename);
   pci= gather(datasets.(current_filename).pci.dist);
 end

也就是说,我猜您需要从文件名中删除扩展名 (.mat) 才能使其正常工作。比如你真的有datasets.random_file1.mat.pci.dist吗?

【讨论】:

    【解决方案2】:

    我的做法是:

    fields = fieldname(data)
    data = getfield(data,fields{1})
    

    我使用这种技术来遍历子字段

    pci_field = getfield(data, fields{1})
    pci = gather(pci_field.dist)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-29
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 2018-02-02
      相关资源
      最近更新 更多