Removing numerical data from txt file
이전 댓글 표시
Inport a txt file, and remove all numerical data, have only text left. when importing text file, the text gets surrounded by quotes.
답변 (3개)
Sulaymon Eshkabilov
2023년 2월 18일
편집: Sulaymon Eshkabilov
2023년 2월 18일
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
Sophia Starzynski
2023년 2월 20일
Sulaymon Eshkabilov
2023년 2월 20일
Please shart your text or dat file to give a proper solution or guidance.
Sophia Starzynski
2023년 2월 20일
Sulaymon Eshkabilov
2023년 2월 20일
편집: Sulaymon Eshkabilov
2023년 2월 20일
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)
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
Sulaymon Eshkabilov
2023년 2월 20일
The answer solution is acceptable :)
카테고리
도움말 센터 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!