From: James Tursa on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i18vqc$mp2$1(a)fred.mathworks.com>...
> "Adrian " <bechaotik(a)gmail.com> wrote in message <i18qn8$k58$1(a)fred.mathworks.com>...
> > "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i18nnn$op7$1(a)fred.mathworks.com>...
> > > "Adrian " <bechaotik(a)gmail.com> wrote in message <i18l3c$k9o$1(a)fred.mathworks.com>...
> > > > Suppose I have a string "Take me home, cas<<ountry rosd<<ad."
> > > > I want to write a script to detect each < as a cue for backspace, making the string read as follows: "Take me home, country road."
> > > > deleting the < as well as the preceding character.
> > > > How can this be done?
> > >
> > > One way:
> > >
> > > function s = bsdel(s)
> > > m = numel(s);
> > > g = true(1,m);
> > > n = 0;
> > > for k=m:-1:1
> > > if( s(k) == '<' )
> > > n = n + 1;
> > > g(k) = false;
> > > elseif( n > 0 )
> > > g(k) = false;
> > > n = n - 1;
> > > end
> > > end
> > > s = s(g);
> > > return
> > > end
> > >
> > >
> > > James Tursa
> >
> > Hi James, I am new to Matlab. esp to Functions.
> > where do i insert the to be detected string in the script?
> > cheers
> > adrian
>
> The '<' is hard-coded per your request. If you want to specify the character to detect for backspace, just pass it in. e.g.,
>
> function s = bsdel(s,c)
> m = numel(s);
> g = true(1,m);
> n = 0;
> for k=m:-1:1
> if( s(k) == c ) % c is the "backspace"
> n = n + 1;
> g(k) = false;
> elseif( n > 0 )
> g(k) = false;
> n = n - 1;
> end
> end
> s = s(g);
> return
> end
>
>
> James Tursa

If you have never used functions before, just put the above code in a file called bsdel.m and have that file somewhere on the MATLAB path. Then you can call it just like any other function. e.g.,

mystring = 'Take me home, cas<<ountry rosd<<ad.'
bscharacter ='<'
newstring = bsdel(mystring,bscharacter)

James Tursa
From: Adrian on
Thanks, James. It works like a GEM!
From: us on
"Adrian " <bechaotik(a)gmail.com> wrote in message <i18l3c$k9o$1(a)fred.mathworks.com>...
> Suppose I have a string "Take me home, cas<<ountry rosd<<ad."
> I want to write a script to detect each < as a cue for backspace, making the string read as follows: "Take me home, country road."
> deleting the < as well as the preceding character.
> How can this be done?
>

one of the many solutions

s='Take meX< home, cas<<ountry rosdXX<<<<ad.';
ss='';
while true
ss=regexprep(s,'(\w{1,1}<)','');
if isequal(ss,s)
break;
end
s=ss;
end
disp(s); % <- or SS...
% Take me home, country road.

us
From: Matt Fig on
Yet another:


% DATA
STR = 'Take me homeuuu<<<, cas<<ountry rosd<<ad.'

% ENGINE
[Y,J] = regexp(STR,'<+','start','end');
H = 2*(J - Y + 1);
Y = Y - H/2;
YJ = cumsum(H);
V = ones(1,YJ(end));
V(1) = Y(1);
V(1+YJ(1:end-1)) = Y(2:length(Y)) - J(1:length(Y)-1);
STR(cumsum(V)) = ''
From: Jan Simon on
Dear Adrian,

It is easy to pretend a solution:
S = 'Take me home, cas<<ountry rosd<<ad.';
S2 = strrep(S, '<', char(8));
disp(S2)
>> Take me home, country road.

But the truth is revealed by inspecting S2:
length(S2)
>> 35
So the backspaces (CHAR(8)) and the shadowed characters are still existing.

I didn't find a solution to read the cleaned printed string. Neither SPRINTF, nor EVALC(DISP) nor FPRINTF to a file or the command window worked - the CHAR8) are still there. I had success using the java.awt.robot simulating keyboard clicks, but this looks even more puzzling than Matt Figs solution ("as someone had rolled an angry armadillo over the keyboard").

Kind regards, Jan