how get over ' | ' ?

hi,
this is sample of my data:
"1|Toy Story (1995)|01-Jan-1995||"
I wrote this code to get over '|' in these data :
%%%%%%%%%
f=fopen('u2.txt','r');
b=fscanf(f,'%s');
for i=1:length(b)
if b(i)=='|'
b(i)=[];
end
end
%%%%%%%%%%%%
but, I got error
how I get over this character '|'?
thanks

回答 (2 件)

bym
bym 2011 年 9 月 10 日

0 投票

one possible solution without for loop
b(b=='|')=[]
b =
1Toy Story (1995)01-Jan-1995
[EDIT]
b =
1|Toy Story (1995)|01-Jan-1995||
for i=1:length(b) if b(i)=='|' b(i)=' '; end; end
b =
1 Toy Story (1995) 01-Jan-1995

3 件のコメント

huda nawaf
huda nawaf 2011 年 9 月 10 日
thanks, i get it.
but if I want use if for another reason ,what can I do?
bym
bym 2011 年 9 月 10 日
you can use the if statement to insert a space ' ' rather than deleting the character with []. That way, the length of b stays the same and the for loop completes without error
Fangjun Jiang
Fangjun Jiang 2011 年 9 月 11 日
Or, do the for-loop backward.
for i=length(b):-1:1
if b(i)=='|'
b(i)=[];
end
end

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

Jan
Jan 2011 年 9 月 12 日

0 投票

Another solution:
b = strrep(b, '|', '');

タグ

質問済み:

2011 年 9 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by