Delete the rows with its first character other than a number and copy all the other rows into a new file.
1 次查看(过去 30 天)
显示 更早的评论
I want to delete the rows with the first character other than a number and copy the other rows which has it's first as a number into a new file.
For ex . my file looks like this
Mach-1 asrt asrj ssrj . . . 0346 1346 2346 3334 4337 . . . aaerh baer daer frah . . . 1345 2346 3436 4346 . . . asdh bsth
I only wan the lines which starts with a number
2 个评论
Michael Haderlein
2014-7-29
That sounds as if you want to solve this again, although it was extensively answered by Azzi Abdelmalek and me (<http://www.mathworks.de/matlabcentral/answers/143308-i-have-to-detect-the-startrow-and-endrow-from-a-file-automatically-from-a-txt-file)>. In case it's the same question, please respond in the other thread.
In case it's a different question because now your file has numbers and text mixed:
fid=fopen(filename);
curline=fgetl(fid);
result=[];
while ischar(curline)
if ~isempty(curline) && ~any(isstrprop(curline,'alpha'))
result=[result;str2num(curline)];
end
curline=fgetl(fid);
end
fclose(fid);
采纳的回答
Azzi Abdelmalek
2014-7-29
编辑:Azzi Abdelmalek
2014-7-29
fid = fopen('file.txt');
a=textscan(fid,'%s')
fclose(fid);
b=a{:};
c=b(cellfun(@(x) ~isempty(regexp(x,'^\d','match')),b))
To export the new data to a new file
fileID = fopen('file1.txt','w');
for k=1:numel(c)
fprintf(fileID,'%s \n',c{k});
end
fclose(fileID);
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!