【问题标题】:Use Value of String Array to name a variable (Matlab)使用字符串数组的值来命名变量(Matlab)
【发布时间】:2021-03-14 07:43:23
【问题描述】:

我正在尝试将多个文件的结果合并到一个结构中。

所述结构应命名为以下名称:

result.[FileName1].Result1ofFile1
result.[FileName1].Result2ofFile1
result.[FileName2].Result1ofFile2
...
result.[FileNameX].ResultYofFileX

我将文件名保存在一个字符串数组中。因此

FileName(1) = "abc1"
FileName(2) = "abc2"
and so on

很遗憾,我不知道如何让它发挥作用。

最后它应该是这样的,不需要自己输入 abc-Names:

result.abc1.Result1ofFile1
result.abc2.Result1ofFile2

如何使用存储在数组中的字符串作为变量名?

【问题讨论】:

    标签: arrays matlab struct naming


    【解决方案1】:

    您可以使用dynamic struct field names

    >> result = struct;
    >> FileName = 'test';
    >> result.(FileName) = rand(10);
    result = 
      struct with fields:
    
        test: [10×10 double]
    

    或者当使用字符串数组时:

    result = struct; 
    for k = 1:numel(FileName)
        result.(FileName(k)) = rand(10);
    end
    

    【讨论】:

    • 非常感谢!这解决了我的问题:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 2013-04-03
    • 1970-01-01
    相关资源
    最近更新 更多