Delete the rows with its first character other than a number and copy all the other rows into a new file.

1 view (last 30 days)
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 Comments
Michael Haderlein
Michael Haderlein on 29 Jul 2014
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);
Vinit
Vinit on 29 Jul 2014
sir .... both the programs works perfectly for me.... i have
one more problem which i will post it as another question.... Thank you for your time

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 29 Jul 2014
Edited: Azzi Abdelmalek on 29 Jul 2014
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);

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!