How to save the value of some variables in a .dat file inside a nested loop in the function environment
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Stephen23
2020-3-1
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 个评论
Stephen23
2020-3-1
编辑:Stephen23
2020-3-1
"The data is separated with commas, is this suitable to be read by MatLab in other script"
Yes, a comma-separated file can be trivially imported into MATLAB using csvread.
Comma-separated files are a very common format for storing data in a text file:
If you want to export/import data using a space delimiter, then use dlmwrite and dlmread.
Please remember to accept my answer if it helped you to resolve your original question.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!