Cut and re-join strings

Hello, I am trying to create a function that will cut a section out of a string and join the two remaining parts togather.
I am getting some weird results towards the end
here is my code
pat = 'MATLAB';
cliplength = length(pat);
stringinput = 'D:\Documents and Settings\212068586\My Documents\MATLAB\help'
endpoint = regexp(stringinput, pat);
newstringstart = [];
newstringtail = [];
for w = 1:length(stringinput)
if w < endpoint
newstringstart(w) = stringinput(w);
end
end
if (((endpoint + cliplength) < w) && (w > endpoint))
for h = (endpoint + cliplength):length(stringinput)
newstringtail(h) = stringinput(w);
end
%newstring(w) = stringinput(w - (endpoint + cliplength));
end
disp(char(strcat(newstringstart, newstringtail)))
Any ideas why it isn't working?
Thanks
Bill

 Risposta accettata

Matt Fig
Matt Fig il 21 Giu 2011
Rather than using a verbal description, show us what you want. For example, if you start with these:
pat = 'MATLAB';
stringinput = 'D:\Documents and Settings\212068586\My Documents\MATLAB\help'
what do you want to have at the end?

12 Commenti

William
William il 21 Giu 2011
Basically I would like to cut the '\MATLAB' out and slide the two remaining parts togather.
Thanks
Matt Fig
Matt Fig il 21 Giu 2011
Ah, now that was easy!
regexprep(stringinput,'\MATLAB','')
Matt Fig
Matt Fig il 21 Giu 2011
See also the help for STRREP, which would do the same thing.
strrep(stringinput,'\MATLAB','')
Walter Roberson
Walter Roberson il 21 Giu 2011
Agggh, the quote-eater got me!
strrep(stringinput,'\MATLAB\,'\')
instead of what Matt and Fangjun Jiang suggested. Otherwise if your username was MATLABUSER your output would become 'D:\Documents and Settings\USER\My Documents\MATLAB\help' instead of your desired 'D:\Documents and Settings\MATLABUSER\My Documents\help'
Matt Fig
Matt Fig il 21 Giu 2011
Good catch, Walter. Now lets hope that there aren't two directories named MATLAB...
strrep(stringinput,'\MATLAB\','\')
If there are two directories named MATLAB, you could use FINDSTR to find their indices, then replace whichever one you want using the appropriate index into the original. For example:
stringinput = 'D:\Documents and Settings\MATLAB\My Documents\MATLAB\help'
I = findstr(stringinput,'\MATLAB\')
stringinput(I(2):I(2)+6) = []
Fangjun Jiang
Fangjun Jiang il 21 Giu 2011
Guys, you seem to have too much fun. What about my advise on using path related functions.
Matt Fig
Matt Fig il 21 Giu 2011
Fangjun, that would be fine, but I was going for something more general - string manipulation.
Fangjun Jiang
Fangjun Jiang il 21 Giu 2011
That is my point. William has been asking about a few string manipulation questions but his example always show file path. That is why I asked him about his overall task.
Walter Roberson
Walter Roberson il 21 Giu 2011
fileparts() was recommended to William in a previous question but he chose to go with his much more complex and non-obvious code.
On the other hand, perhaps now that he sees how messy it can be to do such things properly with string manipulation, he will reconsider using fileparts()
Speaking of messiness: did anyone mention that in modern MS Windows, '/' is just as valid a path separator as '\' is, and that one should thus be prepared for the possibility that one might be dealing with (for example) 'D:\Documents and Settings\31415926\My Documents/MATLAB/help' ...
Fangjun Jiang
Fangjun Jiang il 21 Giu 2011
I did not know that but apparently it is, both in MATLAB and Windows Explorer. Is it good or bad?
Matt Fig
Matt Fig il 21 Giu 2011
I just tried it in Windows 7 and you are correct in at least one sense. I pasted:
C:/Users/matt fig/Desktop/General
into the location bar of an open folder and was taken to the correct location. However, the OS immediately changed the location to:
C:\Users\matt fig\Desktop\General
So I wonder if an inquiry from MATLAB would ever really return a string with / in it?
Walter Roberson
Walter Roberson il 21 Giu 2011
Yes, some inquiries from MATLAB return or directory names as passed in to MATLAB rather than canonicalizing the paths.
I do not know if cd() or uigetdir() on Windows could ever be convinced to return a \ delimited name. Possibly so, see the last paragraph or so of the below-referenced blog.
This state of accepting / or \ has been part of Windows from the beginning: it dates right back to the introduction of directories in DOS. See the following blog from a Microsoft Employee: http://blogs.msdn.com/b/larryosterman/archive/2005/06/24/432386.aspx

Accedi per commentare.

Più risposte (1)

Fangjun Jiang
Fangjun Jiang il 21 Giu 2011

0 voti

Bill, What type of task are you dealing with? I suggest you changing your mind set a little bit. Based on the description part of your question, would this solve your problem? Try not to think of the string at one letter a time.
strrep(stringinput,'MATLAB','')

2 Commenti

William
William il 21 Giu 2011
I am dealing with strings that are file locations c\: ..... place.... and need to cut and paste some strings to look in subfolders and directories. I have been tasked with trying to continue on where a very talented MATLAB programmer left off and have been pretty frusterated.
The script I wrote is for a function I am writing to try and figure out how to cut and paste strings togather. Thank you for all your help
Bill
Fangjun Jiang
Fangjun Jiang il 21 Giu 2011
If you need to deal with the full path of a file or directory, then using related function makes more sense then processing it as ordinary string. Use the following functions:
[PathStr]=fileparts(stringinput)
and you repeat it again and again
fileparts(PathStr)
Other functions are:
fullfile()
exist(PathStr,'file')
exist(PathStr,'dir')
which(Path)
pwd
cd

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by