manipulating text files
15 次查看(过去 30 天)
显示 更早的评论
I have a .txt file structured as below:
title
name 00xxxx yyyy
name 00xxxx yyyy
in each line the first n characters describe a name, followed by 67-n spaces, then a 6 digit code starting with 2 zeros and after 9 spaces a 4 digit codes. What i need to do is copy the xxxx numbers to the place of the yyyy numbers in all lines of the file and save it back to .txt file by maintaining the exact structure in the output file. How could i handle something like that in matlab ?
Thanks in advance
0 个评论
采纳的回答
TAB
2011-9-19
fi=fopen('YourFile.txt','r');
fo=fopen('outfile.txt','w');
txtcell=textscan(fi,'%s','delimiter','\n');
Str1Idx=1;
Str2Idx=68;
Str3Idx=82;
StrLen=86;
for i=1:length(txtcell{1})
if(length(txtcell{1}{i})==StrLen)
txtcell{1}{i}((Str3Idx):(Str3Idx+4))=txtcell{1}{i}((Str2Idx+2):(Str2Idx+6));
end
fprintf(fo,strcat(txtcell{1}{i},'\n'));
end
fclose(fi);
fclose(fo);
4 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!