How Can I load N .txt files with different name in a for loop?

1 回表示 (過去 30 日間)
Gianmarco Manzo
Gianmarco Manzo 2019 年 10 月 17 日
コメント済み: Stephen23 2019 年 10 月 22 日
Hi all,
I have to load some .txt files from different subdirectory. The files name is the same for all of them, only the subdirectory's name changes. I have wrote this code but I have a problem: at each cycle the for loop overwrite the imported matrix. How Can I rename differently the imported matrices? Thx a lot!
SS=string(ones(N,1)); %declare matrix
s=(pwd); %check path
for i=1:N
SS(i,1)= sprintf('Risultati%d', i);
ss(i)=strcat(s,{'\'},SS(i),{'\'},{'disp.out'});
importdata(ss(i)); %%i can use also load function
end

回答 (2 件)

Prasad Parameswaran
Prasad Parameswaran 2019 年 10 月 22 日
MATLAB most useful and powerful feature is its ability to work with arrays. It is better to store the imported matrices in a single array and keep appending the matrices as each file is being imported. You can access a matrix from a particular file by proper indexing.

Gianmarco Manzo
Gianmarco Manzo 2019 年 10 月 22 日
ThX.. I've used struct variable.

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by