Reading multiple files in multiple loops

Hello,
I have a set of text fiels to read into matlab. For instance, to read following files;
cbeta1_1.txt
cbeta1_5.txt
cbeta1_10.txt
cbeta1_20.txt
cbeta2_1.txt
cbeta2_5.txt
cbeta2_10.txt
cbeta2_20.txt
cbeta3_1.txt
cbeta3_5.txt
cbeta3_10.txt
cbeta3_20.txt
I've created the code below, but it only saves the last file to all elements of "mydata", where as I'd like it to write each file sequentially. i.e, cbeta1_1.txt to be mydata{1,1}, and cbeta1_5.txt to be mydata{1,2} and so on.
Any help is much appreciated!
mydata = [];
path = 'H:\..';
for k = 1:3
for i = [1 5 10 20]
for g = 1:12
myfilename{g} = fullfile(path, sprintf('cbeta%d_%d.txt', k, i ));
mydata{g} = importdata(myfilename{g});
end
end
end

 Accepted Answer

The separate ‘g’ loop is not necessary, since it can be created either using a counter or by calculating it.
See if this does what you want —
mydata = cell(12,1);
path = 'H:\..';
iv = [1 5 10 20];
for k = 1:3
for i = 1:numel(iv)
g = k+3*(k-1) + i-1;
myfilename{g,:} = fullfile(path, sprintf('cbeta%d_%d.txt', k, iv(i) ));
% mydata{g,:} = importdata(myfilename{g});
end
end
myfilename
myfilename = 12×1 cell array
{'H:\../cbeta1_1.txt' } {'H:\../cbeta1_5.txt' } {'H:\../cbeta1_10.txt'} {'H:\../cbeta1_20.txt'} {'H:\../cbeta2_1.txt' } {'H:\../cbeta2_5.txt' } {'H:\../cbeta2_10.txt'} {'H:\../cbeta2_20.txt'} {'H:\../cbeta3_1.txt' } {'H:\../cbeta3_5.txt' } {'H:\../cbeta3_10.txt'} {'H:\../cbeta3_20.txt'}
I commented-out the ‘mydata’ assignment since there are no files to read here. Restore it in your code.
.

6 Comments

Thanks for your reply. Yes, this works for this particular example. However, if "iv" is changed to, let's say, [1 5 10 20 30], it will not work.
Can you think of any other more generic way?
Thanks.
That is all the original code was designed to do. There was no mention of ‘iv’ or the upper limit of ‘k’ changing.
It has to be changed slightly to allow for the changed size of ‘iv’, however that is a minor tweak. This will work for any length of ‘iv’ and higher limits of ‘k’ as demonstrated —
mydata = cell(12,1);
path = 'H:\..';
iv = [1 5 10 20 30 50];
km = numel(iv)-1;
for k = 1:4
for i = 1:numel(iv)
g = k+km*(k-1) + i-1;
myfilename{g,:} = fullfile(path, sprintf('cbeta%d_%d.txt', k, iv(i) ));
% mydata{g,:} = importdata(myfilename{g});
end
end
myfilename(1:12)
ans = 12×1 cell array
{'H:\../cbeta1_1.txt' } {'H:\../cbeta1_5.txt' } {'H:\../cbeta1_10.txt'} {'H:\../cbeta1_20.txt'} {'H:\../cbeta1_30.txt'} {'H:\../cbeta1_50.txt'} {'H:\../cbeta2_1.txt' } {'H:\../cbeta2_5.txt' } {'H:\../cbeta2_10.txt'} {'H:\../cbeta2_20.txt'} {'H:\../cbeta2_30.txt'} {'H:\../cbeta2_50.txt'}
myfilename(13:24)
ans = 12×1 cell array
{'H:\../cbeta3_1.txt' } {'H:\../cbeta3_5.txt' } {'H:\../cbeta3_10.txt'} {'H:\../cbeta3_20.txt'} {'H:\../cbeta3_30.txt'} {'H:\../cbeta3_50.txt'} {'H:\../cbeta4_1.txt' } {'H:\../cbeta4_5.txt' } {'H:\../cbeta4_10.txt'} {'H:\../cbeta4_20.txt'} {'H:\../cbeta4_30.txt'} {'H:\../cbeta4_50.txt'}
.
Great! Thanks a lot!
As always, my pleasure!
.
Just out ouf curiosity, is there any logic that you follow for the definition of "g"? or you just come up with an expression? For example, how would you do if there was one more loop for more complicated file names? For example, instead of cbeta[k]_[iv(i)], if we have kappa_[j]_cbeta[k]_[iv(i)], and j consists non-sequential numbers again (let's say j= 5 10 15 20 25). How do we include "j" in g's definition in this case?
On a seperate note, I really appreciate your prompt replies and invaluable contribution to the community!
There is logic. The logic is simply calculating the value based on the indices. (A simple counter would also work, however I prefer the approach I use here.)
Here, the expression increments the ‘g’ value by adding ‘km’, the number of elements of ‘iv’, to ‘k’ so that it adds the value of ‘km’ to ‘k’ in each second and subsequent iteration of the ‘k’ loop value, to the index value in the ‘i’ loop to calculate.‘g’. So in the first ‘k’ iteration, ‘g’ is just ‘k+i-1’, in the second iteration, ‘k+km+i-1’ , in the third iteration, ‘k+2*km+i-1’ and so on. It must be consistent with the loop counter values, since they are used to create the file names.
.

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!