我有一个数据集,我想根据数据集中一列中的值对它进行分类并存储在一个结构中。例如,数据可以分类为元素“label_100”、“label_200”或“label_300”,如下所示:
%The labels I would like are based on the dataset
example_data = [repmat(100,1,100),repmat(200,1,100),repmat(300,1,100)];
data_names = unique(example_data);
%create a cell array of strings for the structure fieldnames
for i = 1:length(data_names)
cell_data_names{i}=sprintf('label_%d', data_names(i));
end
%create a cell array of data (just 0's for now)
others = num2cell(zeros(size(cell_data_names)));
%try and create the structure
data = struct(cell_data_names{:},others{:})
这失败了,我收到以下错误消息:
“错误使用结构字段名称必须是字符串。”
(另外,有没有更直接的方法来实现我上面想要做的事情?)