start to write for a specified location using fprintf

조회 수: 14 (최근 30일)
sermet OGUTCU
sermet OGUTCU 2021년 7월 12일
댓글: sermet OGUTCU 2021년 7월 13일
FileName1='data_file';
result_1=23.9;
FileName2='data_file2';
result_2=21.9;
startingFolder='C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
startingFolder = pwd;
end
defaultFileName=fullfile(startingFolder, '*.txt');
[baseFileName, folder]=uiputfile(defaultFileName, 'Select a file');
if baseFileName == 0
return
end
fullFileName = fullfile(folder, baseFileName); %fullfile=building file
fid = fopen(fullFileName, 'w');
How can I write a text file (starting with line 2) for the results as following:
result_1 result_2
data_file: 23.9 data_file2: 21.9

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 7월 12일
Here's a solution. Make sure you use "r+" when opening the file, otherwise you'll overwrite the first line.
fid = fopen('testdata.txt', 'r+'); % open file for reading and writing
fgetl(fid); % skip over 1st line
fprintf(fid','data_file: %.1f data_file2: %.1f\n', result_1, result_2);
fclose(fid);
  댓글 수: 5
Walter Roberson
Walter Roberson 2021년 7월 12일
When you switch between reading (fgetl() in this case) and writing (fprintf() in this case), it is necessary to perform an fseek() operation to inform the operating system to flush buffers. It is acceptable to fseek() by 0 bytes relative to the current position: you do not have to move the current position, just call fseek() to prepare for the switch.
fid = fopen('testdata.txt', 'r+'); % open file for reading and writing
fgetl(fid); % skip over 1st line
fseek(fid, 0, 'cof'); %prepare to switch from read to write
fprintf(fid','data_file: %.1f data_file2: %.1f\n', result_1, result_2);
fclose(fid);
sermet OGUTCU
sermet OGUTCU 2021년 7월 13일
Dear @Walter, thank you very much for the clarification.

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

추가 답변 (1개)

Scott MacKenzie
Scott MacKenzie 2021년 7월 12일
fid = fopen('testdata.txt', 'w'); % change, as needed
fprintf(fid, ' result_1 result_2\n');
fprintf(fid','data_file: %.1f data_file2: %.1f\n', result_1, result_2);
fclose(fid);
  댓글 수: 5
Scott MacKenzie
Scott MacKenzie 2021년 7월 12일
@Walter Roberson Thanks for pointing this out. Is this documented anywhere? I don't see any mention of this in the fseek documentation.
Walter Roberson
Walter Roberson 2021년 7월 12일
POSIX requirement.
When a file is opened with update mode ( '+' as the second or third character in the mode argument), both input and output may be performed on the associated stream. However, the application shall ensure that output is not directly followed by input without an intervening call to fflush() or to a file positioning function ( fseek(), fsetpos(), or rewind()), and input is not directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by