Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Write to file is not formatting properly

1 view (last 30 days)
Ahsan  Khan
Ahsan  Khan on 19 Aug 2017
Closed: MATLAB Answer Bot on 20 Aug 2021
I have a code where I have some calculations being performed 4 times in a for loop. For each iteration of the for loop, I want the data saved to a column in a .asc file. Note, I am omitting much of the code before this part.
for z=1:length(NBvec)
NB=NBvec(z);
E=(hb*kf).^2/(2*meff);
tau=(pi/gv*E.*NB*a./n.*(FB./((1-G).*FC+2*kf/qs).^2)).^-1;
muBI_13a = (e*tau/meff)*1.3;
loglog(n(1,:)*10^-15,hb*muBI_13a(1,:));
muBI_13a;
length(muBI_13a)
fprintf(fileID,'%5.2f,%5.2f,%5.2f,%5.2f\r\n',muBI_13a');
%axis([1 12 1 50])
legend('1', '4', '16', '64')
box
end
fprintf(fileID,'%5.2f,%5.2f,%5.2f,%5.2f\r\n',muBI_13a');
fclose(fileID);
box
My saved file looks like below
x x x x
x x x x
x x x x x
x x x x
x x x x
x x x x x
x x x x
x x x x
x x x x
x x x x x
x
Not sure how to get it to give me all my data properly in the 4 columns, without adding an extra column every now and then, and having only one column in the last row...

Answers (1)

Image Analyst
Image Analyst on 19 Aug 2017
Not sure why but obviously the size of the matrix is changing from 5 to 4 to 1. Use the debugger to find out why (just like we'd have to do if we had your complete code). http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
  2 Comments
Ahsan  Khan
Ahsan  Khan on 19 Aug 2017
Strangely enough, I am not getting any errors when I run the code, (except when I make a plot but I believe that is unrelated to this particular issue and is an issue with the legend). So far I have tried to manually edit my file, although, this gets to be confusing.
Also perhaps I should mention that, the matrix goes from 4 columns to 5 columns a total of three times, these three extra entries result in three missing entries in the last row of the matrix (it is left as one column)
n=[1:.025:2.5]*10^17;
hb=1.05*10^-34;
m=9.11*10^-31;
meff=.067*m;
e=1.602*10^-19;
gv=1;
kf=sqrt(2*pi*n/gv);
NBvec=[1 4 16 64]*10^20;
epo=8.85*10^-12;
epb=12.9;
aB=4*pi*epo*epb*hb^2/(e^2*meff);
qs=2*gv/aB;
nv=gv;
Q=2*kf;
G=1/(2*nv)*(Q./sqrt(Q.^2+kf.^2));
aA=150;
a=aA*1e-10; %well width
q=2*kf;
FB1=(4*pi^2./(4*pi^2+a^2*q.^2)).^2;
FB2a=(4./(a*q).^2).*(1-7./(4*a*q)-1./(4*a*q).*exp(-2*a*q)+(2./(a*q)+1/2).*exp(-1*a*q));
FB2b=((1-exp(-a*q))./(a*q)).^2;
FB2c=4*((3*(a*q).^2)./(32*pi^2)+1/(2*pi^2)-(1./(q*a)).*((1-exp(-a*q))./(4*pi^2+a^2*q.^2)));
FB2all=(FB2a+FB2b+FB2c);
FB=(1./(q*a));
FC=(1./(4*pi^2+a^2*q.^2)).*(3*a*q+8*pi^2./(a*q)-32*pi^4./(a*q).^2.*((1-exp(-a*q))./(4*pi^2+a^2*q.^2)));
figure(9)
clf(9)
figure(9)
hold on
set(gca, 'XScale', 'log', 'YScale', 'log')
muBI_13a=zeros(length(NBvec))
fileID=fopen('mymu_imp.asc','w');
for z=1:length(NBvec)
NB=NBvec(z);
E=(hb*kf).^2/(2*meff);
tau=(pi/gv*E.*NB*a./n.*(FB./((1-G).*FC+2*kf/qs).^2)).^-1;
muBI_13a = (e*tau/meff)*1.3;
loglog(n(1,:)*10^-15,hb*muBI_13a(1,:));
muBI_13a;
length(muBI_13a);
fprintf(fileID,'%5.2f,%5.2f,%5.2f,%5.2f\r\n',muBI_13a');
legend('1', '4', '16', '64')
box
end
fprintf(fileID,'%5.2f,%5.2f,%5.2f,%5.2f\r\n',muBI_13a');
fclose(fileID);
box
Image Analyst
Image Analyst on 19 Aug 2017
On the first iteration, muBI_13a is a 1 by 61 vector, so since there are only 4 format specifiers in the fprintf() statement, it will keep repeating that until all 61 elements have been printed.

This question is closed.

Community Treasure Hunt

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

Start Hunting!