putting information in a text file

1 view (last 30 days)
stephanie borg
stephanie borg on 30 Mar 2011
So this is my code where i need to take this information in a text file, each one in a seperate line. however it is always starting from 2 lines below the top. I need them to start from the first line. what am i doing wrong?
fid=fopen('simulation.txt', 'at');
fprintf(fid,'%s\n ','injectionmoulding');
fprintf(fid,'% g\n ',y);
fprintf(fid,'% g\n ',x);
fprintf(fid,'% 1.0f\n',-O);
fclose(fid);
fid=fopen('simulation.txt', 'at');
fprintf(fid,'%s\n','drilling');
fprintf(fid,'% g\n',y);
fprintf(fid,'% g\n',x);
fprintf(fid,'% 1.0f\n',-O);
fclose(fid);
file_name = strcat(path, file,'.txt');
V = fileread('simulation.txt');
fid=fopen(file_name, 'at');
fprintf(fid,'%s',V);
fclose(fid);
  4 Comments
Andrew Newell
Andrew Newell on 30 Mar 2011
Platform: Windows 7, Mac OS 10.6, ?

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 30 Mar 2011
It is uncommon to repeatedly fopen() and fclose() the same file. Unless there are some fairly specific reasons, you would normally fopen() it once and leave it open until after the last time you read or write from it.
Could you show us an example of the output you are getting and specifically point out the place the extra lines are occurring?

Fiboehh
Fiboehh on 30 Mar 2011
Hey, i'm also looking for this. My textfile (ascii) must be like this:
testobject // Object name
15 // Number of cells in x-direction
15 // Number of cells in y-direction
1 // Dimension of cell (x-direction)
1 // Dimension of cell (y-direction)
10.3.2011 // Date and time
PDV // Operator
HeNeLaser // Source type
array // Detector type
100 // Half distance source detector [mm]
1 // Number of detectors
0,5 // Half detector size [mm]
-0,0245436926061703 // Rotation angle [rad]
128 // Number of projections
//// slice 1
a variable vector
//// slice 2
other variable vector
//// slice 3
...
//// slice n
other variable vector
0
0
0
Anyone who knows how to do it, save('sino.s2d','-ascii') doesnt works ... thx
  1 Comment
Walter Roberson
Walter Roberson on 30 Mar 2011
Fiboehh, the general scheme you should use should be similar to Stephenie's -- fopen() the file for writing, fprintf() data to the file, and fclose() the file afterwards.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!