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 で String Parsing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!