Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
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 comentarios
Michael Haderlein
Michael Haderlein el 29 de Jul. de 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 el 29 de Jul. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 29 de Jul. de 2014
Editada: Azzi Abdelmalek el 29 de Jul. de 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);

Más respuestas (0)

Categorías

Más información sobre Low-Level File I/O en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by