Creating a text file through Matlab

Hello,
I want to save parameters from a program that I am running to a separate text file, while the program is in progress.
For example, if my program defines (ycent,xcent)=(10,10) I want a text file to be created that writes
"(ycent,xcent)=(10,10)" on the first line.
I want to be able to keep adding data like this to the file, while the program is still running.
Does anyone know how to do something like this?
Thank you in advance for your replies!
Amina

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 8월 24일

0 개 추천

What do you mean your program is in progress? If you want to keep a history of the commands executed in Command Window, use the diary() function.
Otherwise, the M-script or M-function you are running is the text file you want, right?

댓글 수: 5

A
A 2011년 8월 24일
I mean that my program goes on to plot some graphs, etc, and I want to have a text file being populated with the parameters that I feed it while the mother program is still going about it's normal processing... if that makes sense!
Fangjun Jiang
Fangjun Jiang 2011년 8월 24일
In general, you can write anything to a text file.
At the beginning, create a file ID, fid=fopen('MyTextFile.txt,'w+t');
At any time you want to write, fprintf(fid,'ycent=%f;\n'); There are lots of options depending on your format need. help fprintf or sprintf for details.
At the end, fclose(fid);
A
A 2011년 8월 24일
Thank you! I will try it out and then accept the answer.
A
A 2011년 8월 24일
I have having a problem:
If, for example, I do:
fid=fopen('MyTextFile.txt','w+t');
fprintf('Hello %g.\n');
fclose(fid);
nothing is actually written to the text file when I open it through "display" later on. Any idea why this might be happening?
Fangjun Jiang
Fangjun Jiang 2011년 8월 24일
You need to specify fid for the fprintf(), and also provide a number.
fprintf(fid,'Hello %g.\n',1234)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Desktop에 대해 자세히 알아보기

질문:

A
A
2011년 8월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by