fprintf don't print newline character?
조회 수: 4 (최근 30일)
이전 댓글 표시
function pushbutton1_Callback(hObject, eventdata, handles)
outfile = fopen('E:\\out.txt','a');
inputstr = get(handles.txtbox_input,'String');
fprintf(outfile,'%c %s \n',13,inputstr);
fclose(outfile);
Each time I press the push button, I want it to write on a separate line. But It just don't do that. Bot \n and printing the ascii character 13 don't work.
Do you know what I should do?
댓글 수: 1
Fangjun Jiang
2011년 5월 20일
What is the path of your text file? Is it mapping to a drive in a UNIX system. Your code looks fine.
채택된 답변
Jan
2011년 5월 20일
This works fine for me:
File = fullfile(tempdir, 'TestFile.txt');
outfile = fopen(File, 'a');
inputstr = datestr(now, 0);
fprintf(outfile, '%s\n', inputstr);
fclose(outfile);
Does it work for ou? What is the difference to your case? Is inputstr a string or a cell string? You explain, that it "does not work" - but what is happening instead? Do you open the file with the Windows Editor, which is so dull, that it cannot interprete CHAR(10) as line break? Use WordPad or Matlab's editor...
댓글 수: 0
추가 답변 (2개)
Titus Edelhofer
2011년 5월 20일
Hi,
"E:\\" looks like windows: you should open the file as a text file:
outfile = fopen('E:\\out.txt', 'at');
Note the "t" in the second argument. Then it should work just with
fprintf(outfile, '%s\n', 'Hello MATLAB!');
Titus
댓글 수: 1
Jan
2011년 5월 20일
The text mode influences the translation of '\n': In text mode and on Windows, '\n' is written as CHAR([13,10]). All modern editors, even the editor of Matlab 5.3, accept CHAR(10) as line break also. Only the Windows Editor fails to interprete this.
The text mode has some other drawbacks (under Windows, under linux it is simply no difference): A CHAR(27) is interpreted as end-of-file, CHAR(8) is interpreted as BackSpace, CHAR([13,10]) is counted as 1 characters such that FSEEK(FID,0,1), FTELL(FID) can reply unexpected results. There are so many pitfalls in the text mode, that I recommend beginners to avoid it.
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!