Take the 2-minute tour ×

I have a bunch of text files which have both strings and numbers in it, but the string are just in the first few rows.

I'm trying to write a script which go in to my folder search all the file in the folder and delete the text from the files and write the rest as it is in the new text file.

Does anybody know how?

share|improve this question
A sample of the file content would help greatly, don't you think? –  gnovice Apr 8 '11 at 3:34
add comment

2 Answers

I don't think this is a good use of MATLAB.

I think you'd be better off scripting this in Python or shell. Here is one way you could do it with tr in shell if you're on *nix or mac and if your files are all in the same directory and all have the file extension .txt:

for i in `ls *.txt`
cat $i | tr -d "[:alpha:]" > $i.tr.txt

To run. save the code above as a file, make it executable (chmod a+x filename), and run it in the directory with your text files.

share|improve this answer
It would also be helpful if you could give more details about the format of the text files. If they are only in the first fixed number of rows you could use something like the tail command to skip those lines. –  Keith Apr 7 '11 at 22:21
thx I would try this –  user531225 Apr 8 '11 at 3:27
well keith, all text files are in .txt and exact look like eachother with very smal exception. the first four or 5 rows are a actuall text. but after that is lik a 3 column numbers wich I need to ro run in matlab afterwards thats why I want it in matlab. and also the text file is not edited is a raw data file, but I don't know what you mean by first fixed number –  user531225 Apr 8 '11 at 3:35
By fixed number I mean that the number of string lines doesn't change between files. If it is the same, you could use importdata() with the nheaderlines option like this: files = dir('data*.txt'); for i = 1 : numel(files) data = importdata(files(i).name,'nheaderlines',4); %do something with the data end –  Keith Apr 8 '11 at 19:07
add comment

If the number of string lines is always the same, you can use textread() with 'headerlines' option to skip over those string lines, then write the entire text buffer out.

share|improve this answer
that what I thought but I'm not use to matlab and I don't know how to use this. would you minde to explain or give an example –  user531225 Apr 8 '11 at 3:24
add comment

Your Answer


