Removing numerical data from txt file
Show older comments
Inport a txt file, and remove all numerical data, have only text left. when importing text file, the text gets surrounded by quotes.
Answers (3)
Sulaymon Eshkabilov
on 18 Feb 2023
Edited: Sulaymon Eshkabilov
on 18 Feb 2023
Here is one of the possible solutions for this exercise regexprep():
Text=readlines('Citation.txt') % Read data file with texts and numbers
A = regexprep(Text, '\d+(?:_(?=\d))?', '') % All numbers removed
(2) another data file:
Atext=readlines('DATA_TEXT.txt')
A = regexprep(Atext, '\d+(?:_(?=\d))?', '')
3 Comments
Sophia Starzynski
on 20 Feb 2023
Sulaymon Eshkabilov
on 20 Feb 2023
Please shart your text or dat file to give a proper solution or guidance.
Sophia Starzynski
on 20 Feb 2023
Sulaymon Eshkabilov
on 20 Feb 2023
Edited: Sulaymon Eshkabilov
on 20 Feb 2023
Here is the solution:
unzip('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1300600/CONTROLDS2247_SGI_PPCS_Battery_1142022_1218PM_Splice.zip')
A = readlines('CONTROLDS2247_SGI_PPCS_Battery_1142022_1218PM_Splice.txt');
Bnum = regexp(A,'\d*','Match'); % Only numbers
Ctxt = regexprep(A, '\d+(?:_(?=\d))?', ''); % Only texts taken out
% Empty cells are cleaned up
Index1 = (Ctxt(1:end,:)=='-.');
Ctxt(Index1,:)=[];
Index2 = (Ctxt(1:end,:)=='.');
Ctxt(Index2,:)=[] % Only text strings are stored and all empty cells are removed
% Write the cleaned strings into MS Excel
xlswrite('OUT.xlsx', Ctxt)
1 Comment
Sophia Starzynski
on 20 Feb 2023
Here is the solution to keep the time of data collected in the external file:
unzip('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1300600/CONTROLDS2247_SGI_PPCS_Battery_1142022_1218PM_Splice.zip')
A = readlines('CONTROLDS2247_SGI_PPCS_Battery_1142022_1218PM_Splice.txt');
Bnum = regexp(A,'\d*','Match'); % Only numbers
Ctxt = regexprep(A, '\d+(?:_(?=\d))?', ''); % Only texts taken out
% Keep the dates:
Index0 = Ctxt(1:end, :)=='// :: PM';
Ctxt(Index0,:) = A(Index0,:);
% Empty cells are cleaned up:
Index1 = (Ctxt(1:end,:)=='-.');
Ctxt(Index1,:)=[];
Index2 = (Ctxt(1:end,:)=='.');
Ctxt(Index2,:)=[]
% Cleaned data is stored in an external file
xlswrite('OUT.xlsx', Ctxt)
1 Comment
Sulaymon Eshkabilov
on 20 Feb 2023
The answer solution is acceptable :)
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!