Cut and re-join strings

1 visualización (últimos 30 días)
William
William el 21 de Jun. de 2011
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

Respuesta aceptada

Matt Fig
Matt Fig el 21 de Jun. de 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 comentarios
Matt Fig
Matt Fig el 21 de Jun. de 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 el 21 de Jun. de 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

Iniciar sesión para comentar.

Más respuestas (1)

Fangjun Jiang
Fangjun Jiang el 21 de Jun. de 2011
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 comentarios
William
William el 21 de Jun. de 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 el 21 de Jun. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Scope Variables and Generate Names en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by