Clear Filters
Clear Filters

How to delete the words/characters from a .txt file?

44 views (last 30 days)
I have data from a load cell that was written with uncessary characters in a .txt file (Attached). I only need the numbers from the data. Is there anyway to filter the words out? It would be even better if I could then convert that into a .csv file.
For example:
First line says
" Load_Cell_1==51.127 N -82.489 N -73.474 N -0.6313 Nm -0.2440 Nm -1.2160 Nm "
I want it to read
"51.127, -82.489, -73.474, -0.6313, -0.2440, -1.2160"

Accepted Answer

Voss
Voss on 1 Apr 2022
Here's one way to get just the numbers out, which you can then do whatever you want with, including writing to a csv file (using, e.g., writematrix):
% open, read, and close the file:
fid = fopen('loadcell_data.txt');
data = fread(fid,'*char').';
fclose(fid);
% split the text on '==' and 'Nm':
data = split(data,{'==' 'Nm'});
% remove empty element after final 'Nm':
if isempty(data{end})
data(end) = [];
end
% reshape to four columns:
data = reshape(data,4,[]).';
% now the second column of 'data' contains the first four
% columns of numbers in the file, and that still needs
% to be split on 'N' (couldn't do it before because 'Nm'
% also contains 'N').
% split the second column on 'N', combine with columns
% 3 and 4, and convert everything to double:
data = str2double([split(data(:,2),'N') data(:,[3 4])])
data = 25160×6
51.1270 -82.4890 -73.4740 -0.6313 -0.2440 -1.2160 40.0360 9.6510 -61.1110 0.1240 -0.3019 0.0110 51.1000 -82.4470 -73.4840 -0.6306 -0.2439 -1.2160 40.0490 9.6880 -61.1350 0.1245 -0.3021 0.0106 51.0980 -82.4720 -73.4950 -0.6307 -0.2443 -1.2166 40.0620 9.6940 -61.0980 0.1243 -0.3024 0.0107 51.1240 -82.4620 -73.4960 -0.6314 -0.2437 -1.2154 40.0370 9.6920 -61.1110 0.1246 -0.3023 0.0111 51.1170 -82.5080 -73.4930 -0.6312 -0.2442 -1.2159 40.0650 9.6470 -61.0970 0.1247 -0.3028 0.0107

More Answers (2)

per isakson
per isakson on 1 Apr 2022
Edited: per isakson on 2 Apr 2022
Another way. See documentation on textscan(), formatSpec — Format of the data fields
fid = fopen( 'loadcell_data.txt' );
cac = textscan( fid, '%*[^=]==%f%*s%f%*s%f%*s%f%*s%f%*s%f%*s' );
fclose( fid );
num = cell2mat( cac );
num(1:3,:)
ans = 3×6
51.1270 -82.4890 -73.4740 -0.6313 -0.2440 -1.2160 40.0360 9.6510 -61.1110 0.1240 -0.3019 0.0110 51.1000 -82.4470 -73.4840 -0.6306 -0.2439 -1.2160
And
writematrix( num, 'loadcell.csv' ) % or csvwrite()

DGM
DGM on 1 Apr 2022
There are probably faster or more robust ways, but here's one way.
alltext0 = fileread('loadcell_data.txt');
alltext0 = split(alltext0,newline);
numbers = regexp(alltext0,'[\s=]([-\d\.]*) Nm?','tokens');
numbers = vertcat(numbers{:});
numbers = cellfun(@str2double,numbers);
csvwrite('loadcell_data_modified.csv',numbers)

Community Treasure Hunt

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

Start Hunting!