필터 지우기
필터 지우기

Need to save values of a variable into a new column of asc file for each iteration of a for loop

조회 수: 1 (최근 30일)
Ndisvec=[.5 2 8 32 128]*10^11; %Set of density values
figure(1025)
clf(1025)
figure(1025)
hold on
set(gca, 'XScale', 'log', 'YScale', 'log')
%Riemann sum
x=[0:0025:1-.005];
I=[1:length(kf)];
for z=1:length(kf);
kfh=kf(z);
y=((x+(qtf/(2*kfh))).^2.*sqrt(1-x.^2)).^-1;
I(z)=sum(y)*.0025;
end
for z=1:length(Ndisvec)
Ndis=Ndisvec(z);
tau=hb^3*(epo*epb*co)^2/(Ndis*meff*e^4*f^2)*16*pi*kf.^4./(I*sqrt(2));
muDIS1=e*tau/meff;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
loglog(n(1,:)*10^-15,muDIS1(1,:)); %deleted 10^-4
end
figure(1025)
I need to print the values of muDIS1 into a new column for each iteration of the for loop (it will run 5 times). At the end i need an asc file which will have columns of data. I have been playing around with the fopen/fprintf functions but have had no luck. I made a for loop----- for muDIS1(z) and the fprintf just under muDIS1 fclose after i end the loop that didnt work
  댓글 수: 2
Daniel Frisch
Daniel Frisch 2017년 8월 7일
Please give an example of the vector muDis1 for one row and the resulting wanted line in the asc file.
Ahsan  Khan
Ahsan  Khan 2017년 8월 8일
Hi Sprry !
Under Jan Simons answer I have provided an example, his method saves my data but I am unable to format it properly.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2017년 8월 7일
Start with collecting the data:
muDIS1 = zeros(length(Ndisvec), ????) % Set accordingly
for z = 1:length(Ndisvec)
Ndis = Ndisvec(z);
tau = hb^3*(epo*epb*co)^2/(Ndis*meff*e^4*f^2)*16*pi*kf.^4./(I*sqrt(2));
muDIS1(z, :) = e*tau/meff;
loglog(n(1,:)*1e-15,muDIS1(z,:)); %deleted 10^-4
end
Now you can write the matrix at once using fopen, fprintf and flcose. Please try it and post the relevant code, if you still have problems.
PS: Note that 10^-15 is an expensive power operation, while 1e-15 is a cheap constant.
  댓글 수: 3
Jan
Jan 2017년 8월 8일
편집: Jan 2017년 8월 8일
If you do not want a line break after each value, do not insert a line break after each value:
fprintf(fileID, '%5.2f, %5.2f, %5.2f, %5.2f, %5.2f, %d\n', muDIS1);
Omit the useless code "length(muDIS1); muDIS1".
You can either insert the output to the file into the loop, or create matrix as shown in my code and write the file at once, which is expected to be faster.
Using '\r\n' instead of '\n' is needed, if you really want to display the file in the Windows NotePad. All other editors accept '\n' directly for decades.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by