Loading indexed .mat files using a for loop

I would like to know how to load several .mat files using a for loop. My files are as follows T1, T2,... and E1, E2,.... My code is shown below. As you can see I tried using num2str but that didn't work. Also tried strcat to create strings following the file names and then load them. There's probably something I am missing, any help ?
Thank You !
mu_Au = -0.1353383;
mu_Si = 0.1353383;
k_B = 8.6173303e-5;
T = 300;
e0= 1.6021766e-19;
h = 6.62607e-34;
fermi_func = @(E,mu) 1./(1 + exp((E-mu)/(k_B*T)));
for i=1:2
E = load('E'num2str(i) '.mat');
T = load('T'num2str(i) '.mat');
integral = T.*[fermi_func(E,mu_Si) - fermi_func(E,mu_Au)];
I(i) = 2*(e0^2/h)*cumtrapz(E,integral);
s(i) = sum(I);
mu_Au = mu_Au - 0.121;
mu_Si = mu_Si + 0.121;
end

 採用された回答

madhan ravi
madhan ravi 2019 年 1 月 5 日
編集済み: madhan ravi 2019 年 1 月 5 日

1 投票

Forgot the square brackets [ ] and commas , :
E = load(['E',num2str(i),'.mat']);
T = load(['T',num2str(i),'.mat']);
Alternative is to use sprintf():
E = load(sprintf('E%d',i);
T = load(sprintf('T%d',i);

2 件のコメント

Mohammed Darwish
Mohammed Darwish 2019 年 1 月 7 日
Hi Madhan,
Greatly appreciate your help, it worked. Sorry for my late reply !
madhan ravi
madhan ravi 2019 年 1 月 7 日
Anytime :) , no problem.

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

その他の回答 (1 件)

Stephan
Stephan 2019 年 1 月 5 日

0 投票

Hi,
maybe this function from FEX will help to create the needed file names:
Best regards
Stephan

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by