arrays - 使用字符串数组的值来命名变量(Matlab)
问题描述
我正在尝试将多个文件的结果组合到一个结构中。
所述结构应命名为以下名称:
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
如何使用存储在数组中的字符串作为变量名?
解决方案
您可以使用动态结构字段名称。
>> 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
推荐阅读
- gmail-api - 是否可以使用 User.messages.list 同步收件箱消息?
- java - 传递了错误的参数。爪哇
- javascript - 如何让 if/else 语句在函数内部工作
- sql-server - 授予创建数据库的访问权限,但不授予对现有数据库的访问权限
- kubernetes - 可以从 metrics-server 获取哪些指标用于 Horizontal Pod Autoscaling
- ios - Swift:使用 NSNotification 更新 Button.setTitle 和当前 UIPageView
- python - 如何使用列表操作字典并插入另一个字典
- html - 使用VBA从Excel中的网页中提取表格
- java - 当我们在类名中使用“iOS”作为单词时,我们应该将“i”大写吗?
- html - 在登录表单中制作下面的导航链接