Delete the rows with its first character other than a number and copy all the other rows into a new file.
2 ビュー (過去 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!