Write data to text file not containing punctuation marks?

2 views (last 30 days)
Hi there,
Can anyone please tell me how to read data from text file, then write to another file that data but not containing any punctuation marks?
Many thanks!

Accepted Answer

Pruthvi G
Pruthvi G on 12 Mar 2019
Use regexprep or delete the found character using regexp found =[].
Please find attched the code below for Removing all the punctuation marks :
The attached script contains code to read and write without punctautions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Name : Remove_punctuation_from_text_Script.m
% Author : Pruthvi Raj G
% Version : Version 1.0
% Input : Any Text file to read
% Date : 12-Febraury-2019 09:06:00
% Output : Text file Contating no punctuation marks (. , ( ) ! ? - _ [ ] " ')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[textfile,textpath] = uigetfile('*.txt','Please select the text file to Read data From :');
if textfile == 0
warndlg('User Aborted the Process')
return;
end
Read_text_file = fullfile(textpath,textfile);
Write_text_file = fullfile(textpath,regexprep(textfile,'.txt','_without_punctuation.txt'));
fid_read = fopen(Read_text_file,'r');
fid_write = fopen(Write_text_file,'w'); % Keep "a" for appending data to old file.
while ~feof(fid_read)
tline = fgetl(fid_read);
if tline ~=-1
% tline = regexprep(tline,'.',''); % This can be used only when one "." punctuation mark to be removed.
tline(regexp(tline,'[.,?!:"()''[]]'))=[]; % This can be to remove all punctuation marks. [.,)(!"''?-_]
tline = regexprep(tline,'-',' '); % because - has to be removed seperately
fprintf(fid_write,'%s\n',tline) ;
end
end
fclose(fid_read)
fclose(fid_write)
winopen(Write_text_file);
% End of the Script.
INPUT : Sample Text File below with Punctuation Marks.
There are 14 punctuation marks that are commonly used in English grammar. They are the period, question mark, exclamation point, comma, semicolon, colon, dash, hyphen, parentheses, brackets, braces, apostrophe, quotation marks, and ellipsis. Following their correct usage will make your writing easier to read and more appealing.
Sentence Endings
Three of the fourteen punctuation marks are appropriate for use as sentence endings. They are the period, question mark, and exclamation point.
The period (.) is placed at the end of declarative sentences, statements thought to be complete and after many abbreviations.
As a sentence ender: Jane and Jack went to the market.
After an abbreviation: Her son, John Jones Jr., was born on Dec. 6, 2008.
Use a question mark (?) to indicate a direct question when placed at the end of a sentence.
When did Jane leave for the market?
The exclamation point (!) is used when a person wants to express a sudden outcry or add emphasis.
Within dialogue: "Holy cow!" screamed Jane.
To emphasize a point: My mother-in-law's rants make me [furious][][[][][[[][]][!.
OUTPUT : Generated Output after Removing Punctuation Marks.
There are 14 punctuation marks that are commonly used in English grammar They are the period question mark exclamation point comma semicolon colon dash hyphen parentheses brackets braces apostrophe quotation marks and ellipsis Following their correct usage will make your writing easier to read and more appealing
Sentence Endings
Three of the fourteen punctuation marks are appropriate for use as sentence endings They are the period question mark and exclamation point
The period is placed at the end of declarative sentences statements thought to be complete and after many abbreviations
As a sentence ender Jane and Jack went to the market
After an abbreviation Her son John Jones Jr was born on Dec 6 2008
Use a question mark to indicate a direct question when placed at the end of a sentence
When did Jane leave for the market
The exclamation point is used when a person wants to express a sudden outcry or add emphasis
Within dialogue Holy cow screamed Jane
To emphasize a point My mother in laws rants make me furious

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!