MATLAB: How to edit the data file

changeconvertdataeditMATLABtext file

Hello everyone, I am having trouble outputting certain parameters out of my file. I attached the file. I want to be able to create a new file as a .dif file that can be imported into a program where I can graph my information. I want my new file to contain the *MEAS_SCAN information in column 1 with its corresponding info in column 2. For example, I want *MEAS_SCAN_AXIS_X_INTERNAL" "TWOTHETA" the rest of the information is not necessary. I also need my file to contain all the data which I want in (x,y) format. In total, I need to convert my new file into a different file that only has the necessary information.

Best Answer

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.' ); %the transpose is important
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).