2016-09-19 5 views
1

데이터 세트의 한 열에있는 값을 기반으로 구조체에 범주화하고 저장하고자하는 데이터 세트가 있습니다.구조체 필드 이름을 숫자 배열로 작성하십시오.

%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{:}) 

이 실패하고 나는 다음과 같은 오류 메시지가 얻을 :

"오류를 예를 들어, 데이터 요소 'label_100', 'label_200'또는 'label_300'나는 아래의 시도로로 분류 될 수있다 struct 사용하기 필드 이름은 문자열이어야합니다. " documentation of struct에 따르면

(또한, 내가 위에서 할 노력하고 무엇을 달성하기 위해보다 직접적인 방법이있다?)

+0

사용'cell2struct (다른 사람, cell_data_names을)'대신'struct'. –

답변

2

,

S = struct('field1',VALUES1,'field2',VALUES2,...) 지정된 필드가 구조 배열을 생성 및 값.

그래서 필드 이름 바로 뒤에 각 값이 있어야합니다. 당신이 struct를 호출하는 방법은 이제 대신 당신이 해결할 수있는 올바른

S = struct('field1',VALUES1,'field2',VALUES2,...). 

S = struct('field1','field2',VALUES1,VALUES2,...) 

입니다 쉼표로 구분 된 목록을 생성합니다 {:}를 사용하여 다음 수직으로 cell_data_namesothers을 연결하고하여. 이 열 주요 순서로 세포의 내용을 줄 것이다, 그래서 각 필드 이름을 채우기 즉시 해당 값 뒤에는 :

cell_data_names_others = [cell_data_names; others] 
data = struct(cell_data_names_others{:})