How to save the value of some variables in a .dat file inside a nested loop in the function environment

5 views (last 30 days)
Hello everyone,
I am trying to create a .dat file on each for loop of a function enviroment file. The code looks like:
for i=1:length(Tmax0)
for j=1:length(Hy0)
[t,x]=ode45(@(t,x)myode(t,x,Hy0(j),Tmax0(i)),ts,[0 0]);
LX=cos(x(:,1));
LY=sin(x(:,1));
TIME=t;
TT=TEMP(t,Tmax0(i));
mm=(1-TT/Tc).^beta;
wwE=wE0*mm;
MZ=-(1./(2.*wwE)).*x(:,2);
% SAVE DATA HERE
end
end
So what I want to save has to be done where the warning "% SAVE DATA HERE" is placed in the code. I would like to save on each file the value of TIME, LX, LY, and MZ as column vectors, for each iteration on the for loop on j=1:length(Hy0) for all the i=1:length(Tmax0) iterations. It would be great if the name of my file could be controled showing the value of Tmax0(i) and Hy0(j) like... "Laser_Like_Hy0=,'value(Hy0(j))',_Tmax0=,'value(Tmax0(i))'.dat". It would be great if all the decimals of the above variables are saved. Again, just in case that it is important, this code is inside a function environment. I do not if cell2mat works there, for example.
Any suggestions?

Accepted Answer

Stephen23
Stephen23 on 1 Mar 2020
Replace "Save Data Here" with:
fnm = sprintf('Laser_Like_Hy0=%.12f_Tmax0=%.12f.dat', Hy0(j), Tmax0(i));
mat = [TIME(:), LX(:), LY(:), MZ(:)];
csvwrite(fnm,mat)
  3 Comments

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