Guidance on using For Loops for loading data and applying function.

10 ビュー (過去 30 日間)
Daniel Potter
Daniel Potter 2015 年 6 月 5 日
コメント済み: Daniel Potter 2015 年 6 月 5 日
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 件のコメント
Daniel Potter
Daniel Potter 2015 年 6 月 5 日
For anyone from the future looking at this the solution given by Ingrid below works a charm. For the two loops above I used:
for i=1:7;
stringDat = ['./-2d/' num2str(i) '00mm.dat'];
load(stringDat);
eval(['m2d' num2str(i) '= X' num2str(i) '00mm;']);
end
and
for j=1:8;
for i=1:7;
stringDat = ['./' num2str(j) 'd/' num2str(i) '00mm.dat'];
load(stringDat);
eval(['m' num2str(j) 'd' num2str(i) '= X' num2str(i) '00mm;']);
end
end

サインインしてコメントする。

採用された回答

Ingrid
Ingrid 2015 年 6 月 5 日
for loading a dat file you can generate the string of the file as shown below and then use this string directly as input for the load function
to assign the changing variable names you will need to use eval
for i=1:7;
stringDat = ['./-2d/' num2str(i) '00mm.dat'];
load(stringDat);
eval(['m2d(i) = X' num2str(i) '00mm']);
end
  2 件のコメント
Guillaume
Guillaume 2015 年 6 月 5 日
編集済み: Guillaume 2015 年 6 月 5 日
You don't need eval. Just load the data straight into its destination.
I strongly recommend Daniel to avoid eval
Daniel Potter
Daniel Potter 2015 年 6 月 5 日
Thanks very much Ingrid, num2str was very much the droid I was looking for.

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2015 年 6 月 5 日
編集済み: Guillaume 2015 年 6 月 5 日
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 件のコメント
Guillaume
Guillaume 2015 年 6 月 5 日
編集済み: Guillaume 2015 年 6 月 5 日
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
Daniel Potter 2015 年 6 月 5 日
Works grand, and I am able to recover the data to play around with using:
data = cell2mat(md(1,1));
Is there a way to get this inside the loop. The eval command would work again here;
eval(['s',num2str(id),'d',num2str(im),'=cell2mat(md(',num2str(id),',',num2str(im),'));']);
but from what I have been reading this is again not good practice.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by