How to read multiple data files into a dynamically named structure field?
조회 수: 1 (최근 30일)
이전 댓글 표시
I am trying to use dynamic struct names to build a nested structure containing some files. My code so far is as follows:
for jk=1:numel(files)
for idx1=1:length(ids)
for idx2 = 1:length(years{idx1})
data.(ids{idx1}).(['Y' years{idx1}{idx2}])=dlmread(files{jk});
end
end
end
where "files" is a cell array containing ten file names ('L14N_2009.txt', for example, and every .txt file is a large matrix and no two matrices are the same size), and length(ids)+length(years{idx1}) = numel(files) = 10, so dimensions are in agreement. The structure names that are generated are things like data.L14N.2009 and I would like to write each of my ten files into the corresponding innermost fieldname. Can I store each iteration of the for loop so that the first name I'm dynamically creating is a nested structure that stores the output of dlmread(files{1}), the second name created is a structure with dlmread(files{2}),etc.? In other words, can I make it so that data.L14N.2009 = dlmread('L14N_2009.txt'), data.L14N.2010=dlmread('L14N_2010.txt'), etc? Do I need a second for loop? As is, the result of the loop is a structure data.L14N.2009 where the matrix stored is the one that corresponds to files{numel(files)}, obviously. If this was a matrix, I'd do
rvec=rand(3,2)
for i=1:length(rvec)
A(i,:)=rvec(i,:)+1;
end
but alas, I know almost nothing about structures and dynamic names.
댓글 수: 0
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!