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?
|
|
0
|
|
|
|
Reply
|
Adrian
|
7/10/2010 2:15:08 AM |
|
"Adrian " <bechaotik@gmail.com> wrote in message <i18l3c$k9o$1@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
|
|
0
|
|
|
|
Reply
|
James
|
7/10/2010 3:00:07 AM
|
|
"Adrian " <bechaotik@gmail.com> wrote in message <i18l3c$k9o$1@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?
>
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sscanf.html
Not sure, but here's a start. I'm looking into it too...
Maxx
|
|
0
|
|
|
|
Reply
|
Maxx
|
7/10/2010 3:01:03 AM
|
|
"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <i18nnn$op7$1@fred.mathworks.com>...
> "Adrian " <bechaotik@gmail.com> wrote in message <i18l3c$k9o$1@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
|
|
0
|
|
|
|
Reply
|
Adrian
|
7/10/2010 3:51:04 AM
|
|
"Adrian " <bechaotik@gmail.com> wrote in message <i18qn8$k58$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <i18nnn$op7$1@fred.mathworks.com>...
> > "Adrian " <bechaotik@gmail.com> wrote in message <i18l3c$k9o$1@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
|
|
0
|
|
|
|
Reply
|
James
|
7/10/2010 5:18:04 AM
|
|
"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <i18vqc$mp2$1@fred.mathworks.com>...
> "Adrian " <bechaotik@gmail.com> wrote in message <i18qn8$k58$1@fred.mathworks.com>...
> > "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <i18nnn$op7$1@fred.mathworks.com>...
> > > "Adrian " <bechaotik@gmail.com> wrote in message <i18l3c$k9o$1@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
|
|
0
|
|
|
|
Reply
|
James
|
7/10/2010 5:46:05 AM
|
|
Thanks, James. It works like a GEM!
|
|
0
|
|
|
|
Reply
|
Adrian
|
7/10/2010 5:53:05 PM
|
|
"Adrian " <bechaotik@gmail.com> wrote in message <i18l3c$k9o$1@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
|
|
0
|
|
|
|
Reply
|
us
|
7/10/2010 6:20:24 PM
|
|
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)) = ''
|
|
0
|
|
|
|
Reply
|
spamanon (2437)
|
7/10/2010 8:20:05 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
Jan
|
7/10/2010 9:20:21 PM
|
|
"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i1ao6l$msu$1@fred.mathworks.com>...
> 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
but... the above solutions (seems) to work...
% use BS char instead of '<'...
s='Take meX< home, cas<<ountry rosdXX<<<<ad.';
bs=char(8);
s=strrep(s,'<',bs);
ix1=strfind(s,bs);
ss='';
while true
ss=regexprep(s,['(\w{1,1}',bs,')'],'');
if isequal(ss,s)
break;
end
s=ss;
end
ix2=strfind(s,bs);
ix1
ix2
%{
% ix1 =
9 20 21 35 36 37 38
% ix2 =
[]
%}
us
|
|
0
|
|
|
|
Reply
|
us
|
7/10/2010 10:03:04 PM
|
|
Dear us,
> but... the above solutions (seems) to work...
> while true
> ss=regexprep(s,['(\w{1,1}',bs,')'],'');
> if isequal(ss,s)
> break;
> end
> s=ss;
> end
Of course this works. And it is easy to understand, modify and debug (if it would have a bug).
I'm just disappointed, that the simple FPRINTF *can* perform the conversion when printing to the command line, but I cannot catch the result to a string again.
Jan
|
|
0
|
|
|
|
Reply
|
Jan
|
7/10/2010 10:31:05 PM
|
|
"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i1asb9$h6$1@fred.mathworks.com>...
> Dear us,
>
> > but... the above solutions (seems) to work...
> > while true
> > ss=regexprep(s,['(\w{1,1}',bs,')'],'');
> > if isequal(ss,s)
> > break;
> > end
> > s=ss;
> > end
>
> Of course this works. And it is easy to understand, modify and debug (if it would have a bug).
> I'm just disappointed, that the simple FPRINTF *can* perform the conversion when printing to the command line, but I cannot catch the result to a string again.
>
> Jan
but jan...
you simply cannot have if all:
- either germany wins a great(!) third...
- or ML provides a solution to this conundrum...
now, let's see what paul did: he/she/most likely it(!) voted for #1...
:-)
urs
|
|
0
|
|
|
|
Reply
|
us
|
7/10/2010 10:44:06 PM
|
|
How about a drunk and angry armadillo with a hatred of multiple lines?
% DATA
STR = 'Take me homeuuu<<<, cas<<ountry rosd<<ad.'
% ENGINE
STR(cell2mat(cellfun(@(x)x(1)-diff(x)-1:x(2),regexp(STR,'(<)+','tokenExtents'),'Un',0))) = ''
|
|
0
|
|
|
|
Reply
|
Matt
|
7/10/2010 10:56:05 PM
|
|
"Matt Fig" <spamanon@yahoo.com> wrote in message <i1atq4$r6k$1@fred.mathworks.com>...
> How about a drunk and angry armadillo with a hatred of multiple lines?
%{
> % DATA
> STR = 'Take me homeuuu<<<, cas<<ountry rosd<<ad.'
> % ENGINE
> STR(cell2mat(cellfun(@(x)x(1)-diff(x)-1:x(2),regexp(STR,'(<)+','tokenExtents'),'Un',0))) = ''
%}
clearly an armadillo about to loose its drivers license...
:-)
us
|
|
0
|
|
|
|
Reply
|
us
|
7/11/2010 12:38:05 AM
|
|
Dear Matt, dear Us,
> How about a drunk and angry armadillo with a hatred of multiple lines?
>STR(cell2mat(cellfun(@(x)x(1)-diff(x)-1:x(2),regexp(STR,'(<)+','tokenExtents'),'Un',0))) = ''
I know this kind of programming:
Remove the keys from some old laptops, fill them from the wide side into a vuvuzela and shake it. Open the small hole and let one key after the other drop out. Repeat this until the line produces the wanted result (ask the COMBINATOR for the number of drinks you will need).
However, I like this solution, although it does not contain a CUMSUM, but at least CELLFUN and DIFF.
'Un', 0 ))) = ''
What a finale.
Jan
|
|
0
|
|
|
|
Reply
|
Jan
|
7/11/2010 12:42:05 AM
|
|
"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i1b40s$7tj$1@fred.mathworks.com>...
> Dear Matt, dear Us,
>
> > How about a drunk and angry armadillo with a hatred of multiple lines?
> >STR(cell2mat(cellfun(@(x)x(1)-diff(x)-1:x(2),regexp(STR,'(<)+','tokenExtents'),'Un',0))) = ''
>
> I know this kind of programming:
> Remove the keys from some old laptops, fill them from the wide side into a vuvuzela and shake it. Open the small hole and let one key after the other drop out. Repeat this until the line produces the wanted result (ask the COMBINATOR for the number of drinks you will need).
>
> However, I like this solution, although it does not contain a CUMSUM, but at least CELLFUN and DIFF.
> 'Un', 0 ))) = ''
> What a finale.
>
> Jan
And the best part is: its self documenting ;-). LOL
|
|
0
|
|
|
|
Reply
|
Matt
|
7/11/2010 2:01:07 AM
|
|
|
16 Replies
223 Views
(page loaded in 0.428 seconds)
Similiar Articles: replacing character range to remove diacritics - comp.unix.shell ...... to detect the value of the > most significat bit of the character. > The \ character loses its special meaning inside ... last 3 characters of a string ... cut or delete ... How best to detect duplicate values in a column? - comp.databases ...... could add a special field ... doesn't have that character on it. Actualy, for most Latin-1 characters ... appended the string that comes back from mysql_error() after the ... WordStar or CP/M patches for VT100 terminal - comp.os.cpm ...... that it will stop transmitting within 32 ... memory, not doing block moves like character or line insertion or deletion. ... console access, then fire the kamikaze string ... How to remove all subdirectory/file under current directory ...... what I want to > delete before press ENTER. Options after ... directories _are_ special files, after all) doesn't sort before the "-" character ... being capable of detecting ... input & output in assembly - comp.lang.asm.x86... above - e.g. printing strings with a "$" sign inside ... AH = 09h) prints out a string of characters ... won't accept more characters than is specified as the "max character ... Sampling: What Nyquist Didn't Say, and What to Do About It - comp ...... the "reader" more value in this electronic ... that your document is not one long string of characters ... can adjust the kerning and spacing within lines (and character ... Where did Fortran go? - comp.lang.fortran... is still regarded as modern, and coarrays are special ... have to install Lapack to use it in Matlab, it is inside ... It did; and after a few years they shredded what they ... Could anyone give me the spice-mode.el - comp.emacsHi, All I am new to *NIX and I am thinking of writing spice code under Emacs. However, I have no idea of Emacs Lisp. Hence, I could not write a packa... Need a FORTRAN compiler for Win7 (or XP) - comp.lang.fortran ...... however, need to use special flags such as -fno-automatic ... point, and the final character must not be F if the total number of characters ... use of parity bits to detect ... How to check whether malloc has allocated memory properly in case ...The current interface means you don't have to special ... F66 semantics are used: *Statements within a Do ... widget or i/o device, or operations on character strings, and ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ...Archive-name: cdrom/cd-recordable/part1 Posting-Frequency: monthly Last-modified: 2008/10/09 Version: 2.71 Send corrections and updates to And... top 10 uses for random data compression?? anyone? - comp ...... dont think a 10000Ghz CPU will be released within the ... of a little money is to go a video shop after deletion. ... Tell Susan it's tall steering regarding a character. How to Find a Character in a String in VBA | eHow.comFinding a single character in a string using VBA is a special instance of finding one string within another. The process itself uses just one Visual Basic word, InStr ... Characters and Strings (MATLABĀ®)Characters, strings, cell arrays of strings, string comparison, search and replace, character ... for Equality Using Operators Categorizing Characters Within a String 7/29/2012 11:28:42 PM
|