how to remove (.) from strings in matlab

about classify text document

5 件のコメント

James Tursa
James Tursa 2016 年 12 月 16 日
Please give an example. Is it a single string, a cell array of strings? Removing all periods or replacing them with blanks? Etc.
Image Analyst
Image Analyst 2016 年 12 月 16 日
Yeah, clear as mud. Who know? Maybe he has (.) in the strings so he needs to get rid of parentheses too. Needs clarification and examples.
hiba rashed
hiba rashed 2016 年 12 月 17 日
this is the text I live in a city called hilla this city located in.the center of iraq it is not too large hilla contain many resturants in here road.these resturants make very good and delicious food.one of city that near by called najaf
John BG
John BG 2016 年 12 月 17 日
have you tried
L=I{1}
L(L=='.')=[]
Image Analyst
Image Analyst 2016 年 12 月 18 日
Are you sure you don't want to do something like
str = 'I live in a city called hilla this city located in.the center of iraq it is not too large hilla contain many resturants in here road.these resturants make very good and delicious food.one of city that near by called najaf'
dotLocations = find(str == '.')
% Assume tos are periods - end of sentence.
% Make sure the next character is Capitalized
str(dotLocations+1) = upper(str(dotLocations+1))
% Now replace dot with dot space since sentences
% don't start immediately after a period.
str = strrep(str, '.', '. ')
% Make sure the string ends with a period.
if ~strcmp(strtrim(str(end)), '.')
% Get rid of leading and trailing spaces and end with a period.
str = [strtrim(str), '.']
end
Now there are some places where you have a period at places that don't make grammatical sense, like 'in.the' but unless you want to put in a grammar analyzer, it's not possible to know whether to remove that period or replace it with a space.
Same problem for where period are totall missing, like 'of iraq it is not'. Only a full grammatical analysis would know that it's supposed to be 'of iraq. It is not' and to add the period and capitalize the next sentence.

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2016 年 12 月 16 日

2 投票

Use strrep, which is designed to perform the task that you want:
>> str = '.Hello.World.';
>> strrep(str,'.','')
ans = HelloWorld

7 件のコメント

John BG
John BG 2016 年 12 月 16 日
You see dear Cobeldick, if you use strrep, such command has been design precisely to 'replace' so the amount of characters are the same and you are leaving behind spaces where there were dots.
The reading of the question clearly points at 'remove'
remove != replace
oder?
James Tursa
James Tursa 2016 年 12 月 16 日
Huh? Stephen's posted code replaces the periods with an "empty" string ... i.e., it removes the periods instead of replacing them with blanks as you suggest.
>> str = '.Hello.World.';
>> str_no_period = strrep(str,'.','');
>> numel(str)
ans =
13
>> numel(str_no_period)
ans =
10
Stephen23
Stephen23 2016 年 12 月 17 日
編集済み: Stephen23 2016 年 12 月 17 日
John BG wrote:
My code removes the characters, exactly as requested:
>> S1 = 'X.Hello.World.X';
>> S2 = strrep(S1,'.','')
S2 = XHelloWorldX
>> numel(S1)
ans = 15
>> numel(S2)
ans = 12
Nowhere in the MATLAB documentation does it state that with strrep the number of characters must remain the same. As any beginner will know, a simple test will confirm that the number of characters can in fact change:
>> strrep(S1,'.','****')
ans = X****Hello****World****X
John BG
John BG 2016 年 12 月 17 日
yeah,
strrep(str,'.',' ')
and
strrep(str,'.','')
don't look that different from a distance.
I hereby state that strrep with void replacement field '', not ' ', would do the same as my answer
L(L=='.')=[]
Image Analyst
Image Analyst 2016 年 12 月 17 日
Why not?
L = 'a.b.c.d.e'
L(L=='.')=[]
whos L
L = 'a.b.c.d.e'
L = strrep(L, '.', '') % Replace dot with null
whos L
both seem to give the same result. Am I not looking at it correctly?
hiba rashed
hiba rashed 2016 年 12 月 17 日
編集済み: Stephen23 2016 年 12 月 17 日
I have text but it doesn't work this is the code
I = textscan(f,'%s',100);
strrep(I{1},'.','');
I contain this:
I live in a city called hilla
this city located in.the center of iraq
it is not too large hilla contain many resturants
in here road.these resturants make very good and delicious
food.one of city that near by called najaf.
Stephen23
Stephen23 2016 年 12 月 17 日
編集済み: Stephen23 2016 年 12 月 17 日
@hiba rashed: try something like this:
I = strrep(I{1},'.',' ')
I(end) = '.'

サインインしてコメントする。

その他の回答 (1 件)

John BG
John BG 2016 年 12 月 16 日

0 投票

do you mean this?
L='1.- Telegraph http://www.telegraph.co.uk/news/2016/12/13/spain-plans-replace-nazi-time-zone-gmt/';
>> L(L=='.')=[]
L =
1- Telegraph http://wwwtelegraphcouk/news/2016/12/13/spain-plans-replace-nazi-time-zone-gmt/
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

タグ

タグが未入力です。

質問済み:

2016 年 12 月 16 日

コメント済み:

2016 年 12 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by