S = fileread('SOMTEST.txt');
wanted_headers = regexp(S, '^\*MEAS_SCAN.*$', 'match', 'dotexceptnewline', 'lineanchors');
wanted_data_lines = regexp(S, '^\s*-?[0-9.].*?(?=[\r\n])', 'match', 'dotexceptnewline', 'lineanchors');
wanted_data_items = regexp(wanted_data_lines, '\s+', 'split') .';
wanted_data_xyz = str2double(vertcat(wanted_data_items{:}));
wanted_data_xy = wanted_data_xyz(:,[1 2]);
fid = fopen('SOMTEST.dif', 'wt');
fprintf(fid, '%s\n', wanted_headers{:});
fprintf(fid, '%g %g\n', wanted_data_xy.' );
fclose(fid)
This code has been tested and takes into account that the input lines might end in carriage return (\r) followed by newline (\n).
The code allows for the possibility that the numbers might have spaces before them, and that there might be a leading negative. However, if there is a leading negative then the code expects it to be directly beside a digit or period -- the code does not, for example, allow for lines that start "- 3.141" (space after minus).
Best Answer