load .mat files into a loop

2 views (last 30 days)
Miguel Cardoso
Miguel Cardoso on 6 Dec 2019
Commented: Miguel Cardoso on 6 Dec 2019
Good morning!
I have extracted CoTiVGa0.mat, CoTiVGa01.mat, CoTiVGa02.mat ... etc arrays from a previous excel document, and I would like to load them into my script as a loop in order to save the variables minpts, moment, dmdv, Tc and dTcdv into a single vector for several docs. Hope you can help me.
Regards,
Miguel Cardoso
lattice=CoTiVGa0(:,1);
totalenergy_PBE=CoTiVGa0(:,2);
moment=CoTiVGa0(:,3);
Tc=CoTiVGa0(:,4);
polynomialcoefficients_PBE=polyfit(lattice,totalenergy_PBE,3);
linearfunction=linspace(min(lattice)-0.01*min(lattice),max(lattice)+0.01*max(lattice),1000);
d1p = polyder(polynomialcoefficients_PBE); % First Derivative
d2p = polyder(d1p); % Second Derivative
ips = roots(d1p); % Inflection Points
xtr = polyval(d2p, ips); % Evaluate ?d2p? at ?ips?
minpts = ips((xtr > 0) & (imag(xtr)==0)); % Find Minima
energyfit_PBE=polyval(polynomialcoefficients_PBE,linearfunction);
plot(lattice,totalenergy_PBE,'o',linearfunction,energyfit_PBE,'k',minpts,polyval(polynomialcoefficients_PBE,minpts),'or');
xlabel('Lattice Paramenter (\AA)','Interpreter','Latex')
ylabel(' Total Energy (eV)','Interpreter', 'Latex')
legend('SPR-KKR data','Polynomial fit',strcat('Optimized Lattice Parameter(' , num2str(minpts), '\AA)'),'Interpreter','Latex');
axis('tight');
% https://www.mathworks.com/matlabcentral/answers/165219-how-can-i-make-function-and-find-minimum
% Derivatives
moment=CoTiVGa0(:,3);
Tc=CoTiVGa0(:,4);
a1=max(lattice(lattice<minpts));
b1=min(lattice(lattice>minpts));
a2=find(lattice==a1);
b2=find(lattice==b1);
m_a=moment(a2);
m_b=moment(b2);
dmdv=(m_b-m_a)/(b1^3-a1^3); % Needs to be checked
Tc_a=Tc(a2);
Tc_b=Tc(b2);
dTcdv=(Tc(b2)-Tc(a2))/(b1^3-a1^3);
% Magnetic moment and Tc of minpts
Tc_coefs=polyfit(lattice,Tc,1);
Tc_min=Tc_coefs(1)*minpts+Tc_coefs(2);
moment_coefs=polyfit(lattice,moment,1);
moment_min=moment_coefs(1)*minpts+moment_coefs(2);

Accepted Answer

ME
ME on 6 Dec 2019
You could store the filenames for your arrays into an array (possibly cell array might be better if they aren't all the same number of characters. Then stick a for loop around your script and have something like:
FileNames = ['CoTiVGa0.mat',etc]
for i=1:???
load(FileNames(1));
% Rest of your calculations
end
If you need to then you can use clear vars to get rid of the loaded items from your workspace and you can also sotre your results into an array by adding (i) after the names - for example Tc(i)
There are probably a number of other ways of doing this as well.
  1 Comment
Miguel Cardoso
Miguel Cardoso on 6 Dec 2019
Thank you, I will try to implement this idea.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by