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

6 vues (au cours des 30 derniers jours)
Daniel Potter
Daniel Potter le 5 Juin 2015
Commenté : Daniel Potter le 5 Juin 2015
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 commentaire
Daniel Potter
Daniel Potter le 5 Juin 2015
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

Connectez-vous pour commenter.

Réponse acceptée

Ingrid
Ingrid le 5 Juin 2015
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 commentaires
Guillaume
Guillaume le 5 Juin 2015
Modifié(e) : Guillaume le 5 Juin 2015
You don't need eval. Just load the data straight into its destination.
I strongly recommend Daniel to avoid eval
Daniel Potter
Daniel Potter le 5 Juin 2015
Thanks very much Ingrid, num2str was very much the droid I was looking for.

Connectez-vous pour commenter.

Plus de réponses (1)

Guillaume
Guillaume le 5 Juin 2015
Modifié(e) : Guillaume le 5 Juin 2015
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 commentaires
Guillaume
Guillaume le 5 Juin 2015
Modifié(e) : Guillaume le 5 Juin 2015
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 le 5 Juin 2015
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by