필터 지우기
필터 지우기

Function Writing to a text file

조회 수: 1 (최근 30일)
Caroline
Caroline 2014년 10월 12일
답변: Image Analyst 2014년 10월 12일
I am using a function to produce a madlib in a sense and it has to print to the command window and save to a text file. It prints fine to the command window but isn't saving to the text file and no error is showing up. This is what I have so far.
%open file to save to
fid=fopen('story1.txt','w');
%print to file and command window
fprintf(fid,'madlib',y)
fprintf(1,'madlib',y)
%close file
fclose(fid);
mad lib:this is actually a whole story but its very long

채택된 답변

Image Analyst
Image Analyst 2014년 10월 12일
Try it this way instead, with '%s'
% Open a text file to save the string to.
fid = fopen('story1.txt', 'wt'); % Use wt instead of w
% Print string to the file and command window.
fprintf(fid, '%s\n', y) % y is a string. Prints to a file.
fprintf('%s\n', y) % Print to command window instead of a file.
% Close the file;
fclose(fid);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by