start to write for a specified location using fprintf

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

 Risposta accettata

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 Commenti

The output doesn't change when using your second part of the codes. I attached the output text file.
My requirement is that I must start writing at line 2 for an empty text file.
My requirement is that I must start writing at line 2 for an empty text file.
That is not possible in any operating system that MATLAB has been supported on in the last 20 years. The only operating system that such an operation was possible, and which MATLAB was available for, was DEC's VAX VMS operating system. However, MATLAB never supported the operation. VMS supported two completely different modes of writing files: RMS ("Record Management System"), or as byte streams (what every supported operating system uses.) MATLAB never supported making RMS calls. However, out of all of the operating systems MATLAB has ever run on, VMS RMS was the only one that ever supported the possibility of creating a line 2 without also creating a line 1.
Scott's solution does not start with an empty text file: it starts with a text file that is assumed to have at least one line. It reads the line, and then writes a new second line, replacing whatever was there. If there were additional lines already in the file, they might be partly or fully overwritten with the new line, or they will be left in place.
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);
Dear @Walter, thank you very much for the clarification.

Accedi per commentare.

Più risposte (1)

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 Commenti

I need to start for writing in second line. Could you modify your codes to write "result_1 result_2" section in line two in the text file?
Is your requirement that you must write after all existing lines? Or is your requirement that you must start writing at line 2 specifically, even if there are additional lines? If there are additional lines, what do you want to have happen to them? Is the requirement to overwrite line 2, leaving all other lines intact?
OK, got it. See the new solution I just posted.
@Walter Roberson Thanks for pointing this out. Is this documented anywhere? I don't see any mention of this in the fseek documentation.
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.

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by