Read multiple txt files and remove 2nd line with all dashes and output back to files to append _output in the filenames

3 次查看(过去 30 天)
I have multiple .txt files to read and want to remove the 2nd line with all dashes in all files. Then each of them should be saved with the same filename. Or appended with "_output.txt".
I Have the following codes, but it only generate files without contents. Can someone helps?
SBFsample=dir('/Users/ichen/Documents/MATLAB/SBF/*.TXT'); % Identify a directory and TXT files to be merged/processed
for i=1:length(SBFsample)
fid=fopen(SBFsample(i).name,'r'); % Open each data file in turn
fidOut=fopen(SBFsample(i).name,'output.txt','w'); % Open an output file
hdr = textscan(fid,'%s',1,'Delimiter','\n','Whitespace',''); % Capture the header line
fprintf(fidOut,'%s\n',hdr{1}{:}); % Print header file and
for i=1:1, fgetl(fid); end % Skip and remove the dash line remove the dash line on the original data file
fwrite(fidOut,fread(fid,'*char'),'*char') % Read remainder as char* image and echo back out
fclose(fidOut);
fclose(fid); % Complete reading and combining individual file ...
end % Close the output file
  1 个评论
per isakson
per isakson 2017-10-6
Steps
  1. Converted your code to a function. I think it's easier to debug functions than scripts and functions don't litter my base workspace.
  2. Created two sample text files.
  3. Run the function
Result
>> cssm
Error using fopen
Invalid permission.
Error in cssm (line 5)
fidOut=fopen(SBFsample(ii).name,'output.txt','w');

请先登录,再进行评论。

采纳的回答

per isakson
per isakson 2017-10-6
编辑:per isakson 2017-10-6
I run your code successfully after having replaced
fidOut=fopen(SBFsample(i).name,'output.txt','w');
by
fidOut=fopen( [SBFsample(ii).name,'_output.txt'],'w');
The code requires that the text files are in the current directory

更多回答(1 个)

Ivy Chen
Ivy Chen 2017-10-6
This works out beautifully, thanks!

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by