Need to Remove Headers from Large file that wont open in Notepad
Show older comments
Hey! I am new to Matlab and wanting to remove the header names on the file. Usually I would just save as a txt file and delete headers from this file and resave. However, the file is to large to open in Notepad. I want to remove the headers so when when I 'writetable' and save as a txt file there is no headers just three columns with data. Any help is much appreciated! Thank you!

1 Comment
dpb
on 16 Dec 2020
You don't need to do anything to the input file; you can choose to read or not read the headerline when you read the data in and to write or not write the header with writetable all in MATLAB.
Answers (1)
Image Analyst
on 16 Dec 2020
Try this. Though I didn't test it so be careful.
fullInputFileName = fullfilt(pwd, 'blah.txt')
fullOutputFileName = fullfilt(pwd, 'blah.tmp')
if ~isfile(fullInputFileName)
errorMessage = sprintf('File not found:\n%s', fullfileName);
uiwait(errordlg(errorMessage));
return;
end
% Open the input file for reading in text mode.
fileIDIn = fopen(fullInputFileName, 'rt');
% Open the output file for writing in text mode.
fileIDOut = fopen(fullInputFileName, 'wt');
% Read the first line of the file.
textLine = fgetl(fileIDIn);
lineCounter = 1;
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
if lineCounter > 1
% Write second line on down...
fprintf(fileIDOut, '%s\n', textLine);
end
% Read the next line.
textLine = fgetl(fileIDIn);
lineCounter = lineCounter + 1;
end
% All done reading all lines, so close the file.
fclose(fileIDIn);
fclose(fileIDOut);
% Make a copy of the original file as *.bak for safety
backupFileName = strrep(fullInputFileName, '.txt', '.bak');
copyfile(fullInputFileName, backupFileName)
% Delete the original file
recycle on; % Delete to recycle bin for safety
delete(fullInputFileName);
% Rename the output file to the input file.
movefile(fullOutputFileName, fullInputFileName);
For safety I make a backup copy of the input file, and when I delete it (so I can overwrite it with the new headerless file) I put it in the recycle bin. Change .txt to .csv if it's a csv file.
Categories
Find more on Model, Block, and Port Callbacks 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!