Guidance on using For Loops for loading data and applying function.
이전 댓글 표시
I have 11 directories (-2d to 8d) each containing 7 data files (100mm.dat to 700mm.dat). At present in order to load these files in a single script I am doing the following:
load ./-2d/100mm.dat
load ./-2d/200mm.dat
load ./-2d/300mm.dat
load ./-2d/400mm.dat
load ./-2d/500mm.dat
load ./-2d/600mm.dat
load ./-2d/700mm.dat
m2d1 = X100mm;
m2d2 = X200mm;
m2d3 = X300mm;
m2d4 = X400mm;
m2d5 = X500mm;
m2d6 = X600mm;
m2d7 = X700mm;
for each directory. Now this works fine, it gets the data loaded up for me to play around with but my instinct tells me that there is a better way to write this and I was thinking that it may involve a for loop, something like:
for i=1:7;
load ./-2d/(i)00mm.dat;
m2d(i) = X(i)00mm;
end
I know the above is probably nonsense to MATLAB but I am just trying to express the idea I have in my mind. Likewise for the directories would it be possible to use a for loop for that as well, something like:
for j=1:8;
for i=1:7;
load ./(j)d/(i)00mm.dat;
m2d(i) = X(i)00mm;
end
end
Thanks in advance, I hope the above makes sense.
채택된 답변
추가 답변 (1개)
md = cell(11, 7); %rows are the d, columns are the mm
d = -2:8;
m = 100:100:700;
for id = 1:numel(d)
for im = 1:numel(m)
md(id, im) = load(sprintf('./%dd/%dmm.dat', d(id), m(im)));
end
end
will load all your data into a single 2D cell array.
edit: made a stupid error with the indexing.
댓글 수: 2
I'm loading the data files into a 2D cell array exactly for that purpose. The size and shape of each data file does not matter. Each cell of a cell array can contain a matrix of any size.
Try the code exactly as it is posted. The output md should be a 11x7 cell array where each cell is a matrix. For example, md{2, 5} will contain the data for the 500mm file in the -1d directory.
To view the content of the whole cell array you can use celldisp.
Daniel Potter
2015년 6월 5일
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!