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!