Reading multiple files in multiple loops

3 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2021-9-14
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 个评论
Oguzhan M
Oguzhan M 2021-9-14
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!
Star Strider
Star Strider 2021-9-14
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.
.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by