How to include counter in the header?

Hi, I have a big amount of data stored in my file. It has to be formatted in a specific way. After each iteration I have to include G54D and number of iteration, like this G54D11, G54D12...But what I have now is this:
G54D
x 0y-18834D03*
x-69978y-51185D03*
x-18678y 18834D03*
x 51300y 70020D03*
G54D
x 0y 1949D03*
x-22629y-45893D03*
Could you please help to include counter in the header? Code I am using now is:
ct=11; while n<nmax for n=n:nmax; rn=(sqrt(2*n*f*lambda+n^2*lambda^2)); w=(lambda*f/2/rn); d=1.53096*w; phi=asin((d/2)/rn); num=floor(fillfactor/phi); gap=2*(pai/(num-1)-phi); a=rand; theta(1)=pai*a; for i=2:num; theta(i)=theta(i-1)+gap+2*phi; x(i)=1000000*rn*cos(theta(i)); y(i)=1000000*rn*sin(theta(i)); end; ct=ct+1 fileID = fopen('matrix.txt','a+' ); formatSpec = 'x%6.0fy%6.0fD03*\n' fprintf(fileID,'%7s\n','G54D'); fprintf(fileID,'x%6.0fy%6.0fD03*\n',x,y); fclose(fileID); end end;
Thanks!

댓글 수: 1

Sorry, code is:
ct=11;
while n<nmax
for n=n:nmax;
rn=(sqrt(2*n*f*lambda+n^2*lambda^2));
w=(lambda*f/2/rn);
d=1.53096*w;
phi=asin((d/2)/rn);
num=floor(fillfactor/phi);
gap=2*(pai/(num-1)-phi);
a=rand;
theta(1)=pai*a;
x(1)=1000000*rn*cos(theta(1))
y(1)=1000000*rn*sin(theta(1))
for i=2:num;
theta(i)=theta(i-1)+gap+2*phi;
x(i)=1000000*rn*cos(theta(i));
y(i)=1000000*rn*sin(theta(i));
end;
ct=ct+1
fileID = fopen('matrix.txt','a+' );
formatSpec = 'x%6.0fy%6.0fD03*\n'
fprintf(fileID,'%7s\n','G54D');
fprintf(fileID,'x%6.0fy%6.0fD03*\n',x,y);
fclose(fileID);
end
end;

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

답변 (1개)

per isakson
per isakson 2012년 12월 13일
편집: per isakson 2012년 12월 13일

0 개 추천

fprintf( fileID, '%7s%2d\n','G54D', ct );
.
[EDIT] Small test
ct = 11;
fprintf( 1, '%7s%2d\n','G54D', ct );
outputs
G54D11
to the command window

댓글 수: 6

Olha
Olha 2012년 12월 13일
편집: Olha 2012년 12월 13일
Thank you for your answer, but it doesn't work, what I have using your code is this:
G54D
x 0y-18834D03*
x-51300y-70020D03*
G54D
|
x -1949y 22629D03*
x 26415y 0D03*
It just adds 1 row and puts strange characters in there, like |, \
And I have to start numbering from 11, that's why I included separate counter ct, instead of n. Thanks.
per isakson
per isakson 2012년 12월 13일
See above
Olha
Olha 2012년 12월 13일
Yes, I see, it should work. This is strange...I still have the same output. I do not understand why it is adding extra row with strange symbols instead of just adding 11 to the first part? Any ideas why it is happening? Did I place ct in the right place in the code? Can it matter? Thanks!
"strange symbols" might be caused by a problems with the class of the value of the variable, ct. Set a breakpoint and try
class( ct )
or add the line
class( ct )
before the print statement.
Olha
Olha 2012년 12월 13일
:( Same result, class(ct) is double
per isakson
per isakson 2012년 12월 17일
편집: per isakson 2012년 12월 17일
To help, we need to know exactly what code together with input that gives the unexpected result.

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

카테고리

도움말 센터File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

질문:

2012년 12월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by