How to put a header in a txt file?

I'd like to put a header in my .txt file. I have a file with 744 lines like below. And I wanna put on the first line the header 'hora'.
001
002
003
004
005
006
...
744

 Accepted Answer

Straight-forward solution would be like this:
% Read the file
fid = fopen('yourData.txt','r');
str = textscan(fid,'%s','Delimiter','\n');
fclose(fid);
% Add your header
str2 = [{'hola!'}; str{1}];
% Save as a text file
fid2 = fopen('output.txt','w');
fprintf(fid2,'%s\n', str2{:});
fclose(fid2);

6 Comments

shobhit pipil
shobhit pipil on 3 Feb 2019
Edited: shobhit pipil on 3 Feb 2019
Hi,
I am trying to implement your code for multiple text files as input and preserve the file name for each output files.
But I am not able to do that would you please suggest the required corrections to your solution.
Thanks
OK.
Assuming your text files are stored in /Input folder and there is an empty /Output folder, the following code will apply it for all the text files and save them in the /Output folder.
fileList = dir('Input/*.txt');
for kk = 1:numel(fileList)
% Read the file
fid = fopen(['Input/',fileList(kk).name],'r');
str = textscan(fid,'%s','Delimiter','\n');
fclose(fid);
% Add your header
str2 = [{'hola!'}; str{1}];
% Save as a text file
fid2 = fopen(['Output/',fileList(kk).name],'w');
fprintf(fid2,'%s\n', str2{:});
fclose(fid2);
end
shobhit pipil
shobhit pipil on 4 Feb 2019
Edited: shobhit pipil on 4 Feb 2019
Hi Akira Agata,
Thanks for your solution I am now able to see that data is being extraced and saved into files, while name of the files preserved. Yet the records return in cell and I will convert that to matrix as my model takes data as float and header as string.
Thanks
shobhit pipil
shobhit pipil on 4 Feb 2019
Edited: shobhit pipil on 4 Feb 2019
Hi Akira Agata
Now I am using fscanf to read float data from text file, what subsequent code modification do I need to implement as I told earlier that I want to keep first line string and remaining data as float.
% line 20 - str2 = [{'18900101'}; str(:)];
returns 2x1 matrix, where 18900101 saved in 1x1 position and str saved in 2x1 matrix position (all records in single cell)
thanks
Shobhit
Hi Shobhit-san,
Sorry, I don't catch your concern correctly.
Do you want to add a header in a txt data file (as described in the original question)?
Or, do you want to read txt data file as a numeric array?
Anyway, I believe the following slightly modified code can do both of them. I hope this will be some help to address your problem!
fileList = dir('Input/*.txt');
for kk = 1:numel(fileList)
% Read the file
fid = fopen(['Input/',fileList(kk).name],'r');
str = textscan(fid,'%s','Delimiter','\n');
fclose(fid);
% Convert data into numeric matrix
data = str2double(str{1});
% ----------------------------------------------------
% Do something by your model which takes data as float
% ----------------------------------------------------
% Add your header
str2 = [{'hola!'}; str{1}];
% Save as a text file
fid2 = fopen(['Output/',fileList(kk).name],'w');
fprintf(fid2,'%s\n', str2{:});
fclose(fid2);
end

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!