how to read n first lines of a txt file and keep the format when writing the read data to the new (another) text file

48 views (last 30 days)
Hi MATLAB folks,
I have a long .txt file which each line of that has its own specific format, e.g.
contents of a sample text file
created on Sept. 21, 2012
no. day month year
1 Sat Jan 2012
2 Mon Sept 2001
Now, I am looking for a way to read its, let say 4 lines, and keep the format of these lines when I am writing this data into other text file so that I end up with:
contents of a sample text file
created on Sept. 21, 2012
no. day month year
in my new .txt file.
Is there any suggestion?
Many thanks in advance, -V

Accepted Answer

José-Luis
José-Luis on 21 Sep 2012
Edited: José-Luis on 21 Sep 2012
To read into a cell array, line by line:
fid = fopen(your_filename,'r');
numLines = 4;
your_text = cell(numLines,1);
for ii = 1:numLines
your_text(ii) = {fgetl(fid)};
end
fclose(fid);
And now to save:
fid = fopen('your_file.txt','w');
for ii = 1:numLines
fprintf(fid,'%s\n',your_text{ii})
end
fclose(fis)
your_first_value = fscanf(fid,'%d',1);
fclose(fid);
Note that the return of carriage will be '\r\n' in Windows instead of just '\n'.
  1 Comment
Gabriele Giardino
Gabriele Giardino on 11 Apr 2019
I would like to say that this way is really powerful!
I tried to fetch data from the file and to copy them in the same loop, let's say:
% ... Previous code to open fid and fid_down files
% Copying downsampled values
fseek(fid, 0, 'bof');
for idxData = 1:length(idxDownSample)
fseek(fid, 0, 'bof');
currLineData = textscan(fid, '%s', 1, 'delimiter', '\n', 'headerlines', idxDownSample(idxData)-1, 'whitespace', '');
fprintf(fid_down, '%s\n', currLineData{1,1}{1,1});
end
% Adding the **END at the end of the file
fprintf(fid_down, '\n');
fprintf(fid_down, '**END');
% Closing files
fclose(fid_down);
fclose(fid);
But it was really slow while creating the new file fid_down (4-5 kB/s)!
Your solution is different about storing data in MATLAB and then copying them, as in this way:
% QUICK WAY
% Copying downsampled values
fseek(fid, pointMeas, 'bof');
content = cell(length(idxDownSample), 1);
for idxData = 1:length(idxDownSample)
content(idxData) = {fgetl(fid)};
for idxSkipVal = 1:rate-1
fgetl(fid);
end
end
fclose(fid);
for idxData = 1:length(idxDownSample)
fprintf(fid_down, '%s\n', content{idxData});
end
% Adding the **END at the end of the file
fprintf(fid_down, '\n');
fprintf(fid_down, '**END');
% Closing files
fclose(fid_down);
And this way is fat quicker than the previous!
Probably MATLB messes up when it has to switch between opened files, becoming slower when fetching data to copy in other files.
I hope this can be useful for who has tried as me to fetch and copy at the same time between 2 different files

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 21 Sep 2012
Edited: Azzi Abdelmalek on 21 Sep 2012
fid = fopen('file.txt');
tline = fgetl(fid);
out=tline
while ischar(tline)
tline = fgetl(fid);
out=char(out,tline)
end
fclose(fid);

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!