MATLAB: Remove text from a text file without otherwise altering the file

MATLABregexptext filetext;

I need to remove text from a text file without otherwise altering the file INCLUDING other text in the same line. specifically I need the text of the form 'E\S+' (ie "E3453.464") to go away without otherwise altering the line.
so this: G1 X104.650 Y95.350 E4.58979 should become this G1 X104.650 Y95.350
and this G1 E-2.00000 F2400.00000 should become this G1 F2400.00000

Best Answer

You can use regexprep for this purpose.
x1 = 'G1 X104.650 Y95.350 E4.58979';
x2 = 'G1 E-2.00000 F2400.00000';
x1 = regexprep(x1,'(E-?)([0-9]){0,}\.?(\d+)\s?','');
x2 = regexprep(x2,'(E-?)([0-9]){0,}\.?(\d+)\s?','');
x1 = 'G1 X104.650 Y95.350 '
x2 = 'G1 F2400.00000'
The expression:
  • Match E
  • Match '-' optionally
  • Match numbers 0-9 at least one time
  • Match '.' optionally
  • Match as many digits as possible
  • Match whitespace optionally