필터 지우기
필터 지우기

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, ?
stephanie borg
stephanie borg 2011년 3월 30일
7

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

답변 (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