2016-06-09 4 views
0

나는 엄청난 양의 데이터를 추가 처리를 위해 matlab에 가져 오기 위해 필요한 작은 프로젝트를 수행 중이다. 현재 15 개의 엑셀 파일이 있으며 각 파일에는 8 개의 시트가 있습니다. 내가 원하는 것은 각 Excel 파일을 구조체로로드하려는 부모 구조를 만드는 것입니다. parentstructure는 주요 구조 및 파일명구조물에 Excel 파일로드하기

parentstructure.filename.value{} 

상위 구조의 다른 구조는 엑셀 파일이며, 각 파일 셀의 8 장을 가지고 엑셀.

나는 작은 코드를 작성하여 matlab에 데이터를 읽었다. 코드는 다음과 같습니다

srcdir = ''; %%% where all the files are placed 
srcfiles = dir(fullfile(srcdir, '*.xls')); 

for p = 1:numel(srcfiles) 

    filename = fullfile(srcdir, srcfiles(p).name); 
    [~,sheets] = xlsfinfo(srcfiles(p).name); 

    for i = 1:8 
     Sheet = char(sheets(1,i)) ; 
     value{p,i} = xlsread(filename,Sheet); 

    end 
end 

이 코드는 제대로 작동하고 matlab에 데이터를로드하지만 내가 원하는 structrue 형식이 아닙니다. 여러 다른 조합과 조정을 시도했지만 오류가 발생했습니다. 어떤 도움이나 guuide도 많이 감사하겠습니다. 감사합니다.

답변

1

게시 한 코드에 실제로 struct을 만들지 않았습니다. struct 키워드를 사용하면됩니다. 그런 다음 각 파일을 filename 필드에 할당하려면 genvarname (또는 matlab.lang.makeValidName)을 사용하여 파일 이름을 유효한 필드 이름으로 변환하고이 구조체를 할당해야합니다.

% Initialize Parent Structure 
parentStructure = struct(); 

srcdir = ''; %%% where all the files are placed 
srcfiles = dir(fullfile(srcdir, '*.xls')); 

% Sort the files by numbers in their names 
numbers = regexp({srcfiles.name}, '\d+', 'match'); 
numbers = str2double(cat(1, numbers{:})); 
[~, sortind] = sort(numbers); 
srcfiles = srcfiles(sortind); 

for p = 1:numel(srcfiles) 

    % Convert filename to a valid field name 
    fieldname = matlab.lab.makeValidName(srcfiles(p).name); 

    filename = fullfile(srcdir, srcfiles(p).name); 
    [~,sheets] = xlsfinfo(filename); 

    value = cell(1,8); 

    for k = 1:8 
     Sheet = char(sheets(1,k)) ; 
     value{k} = xlsread(filename,Sheet); 
    end 

    % Now assign this struct of sheets to your parentStructure 
    parentStructure.(fieldname) = value; 
end 
+0

데이터가로드되고로드됩니다. 그러나 문제는 파일이 순서대로로드되지 않는다는 것입니다. 이것은 파일 이름이 예를 들어 숫자로 시작하기 때문입니다. 1.xls, 2.xls 등등. 하지만 구조 matlab에 배열하는 방법입니다 1.xls, 10.xls, 11.xls ... 2.xls, 3.xls ..... matlab에 파일을 순서대로 읽는 방법은 없나요? 그들을 조정?. 나머지는 내 코드에 따라 다릅니다. – Muhammad

+0

@Muhammad 위의 코드에 정렬 부분을 추가했습니다 – Suever

+0

thanx @Suever its good – Muhammad