多结构输出到列表

multi structure output to list

下面是一些代码,可用于从结构中提取 ID 名称的元胞数组。它使用 for 循环来实现这一点。但是我想知道是否有一种方法可以在不使用 forloop 的情况下执行相同的任务?

tft(1).Id = 'Name1';
tft(1).Desc = 'goes by the name';
tft(2).Id = 'Name2';
tft(2).Desc = 'hates the name';

for a=1:length(tft)
    list{a} = tft(a).Id
end

有一个专门针对这个问题的文档页面:Access Elements of a Nonscalar Struct Array

因为做 tft.Id returns 一个逗号分隔的列表,你可以通过将它括在花括号中直接将它转换成元胞数组:

list = {tft.Id};