putting information in a text file

2 次查看(过去 30 天)
stephanie borg
stephanie borg 2011-3-30
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 个评论
Andrew Newell
Andrew Newell 2011-3-30
Platform: Windows 7, Mac OS 10.6, ?

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2011-3-30
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 2011-3-30
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 个评论
Walter Roberson
Walter Roberson 2011-3-30
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by