string choppping

조회 수: 4 (최근 30일)
William
William 2011년 6월 17일
Is there a way to chop off part f a sting after a certain set of characters?
If I use strtok( ) It seems to only look at the first character in the deliminter. Is there a way to make the command look at a certain string as a delimiter rather than a char?
Thanks
  댓글 수: 2
William
William 2011년 6월 20일
Here is my code so far
clc
clear
outloc = 'D:\Documents and Settings\212068586\My Documents\MATLAB'
pat = 'MATLAB';
endpoint = regexp(outloc, pat);
newstring = [];
for w = 1:length(outloc)
if w < endpoint
newstring(w) = outloc(w);
end
disp(newstring)
end
I am getting a boatload of nnumbers and am wondering if this is ascii or something??
Thanks
Walter Roberson
Walter Roberson 2011년 6월 20일
Initialize with newstring = '';

댓글을 달려면 로그인하십시오.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 6월 20일
Not sure what your "chopping" try to achieve, could the fileparts() function help you task?
>> [PathStr,Name,Ext]=fileparts(outloc)
PathStr =
D:\Documents and Settings\21286\My Documents
Name =
MATLAB
Ext =
''

추가 답변 (6개)

Walter Roberson
Walter Roberson 2011년 6월 17일
As you are chopping, you could use strfind to get the index (watch out for multiple occurrences) and then use straight indexing.

Fangjun Jiang
Fangjun Jiang 2011년 6월 17일
help strrep
help regexp
help regexprep
If you provide an example, many expert would be eager to try their regular expression skills.

Andrew Newell
Andrew Newell 2011년 6월 17일
token = strtok(str, delimiter)
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 6월 17일
Nope. "If the delimiter input specifies more than one character, MATLAB treats each character as a separate delimiter; it does not treat the multiple characters as a delimiting string"
Andrew Newell
Andrew Newell 2011년 6월 17일
How dare MATLAB conflict with my personal reality? However, the source code for STRTOK would make a good starting point for creating a function that handles longer delimiters.

댓글을 달려면 로그인하십시오.


William
William 2011년 6월 20일
The string I am trying to chop up is this:
>> outloc = 'D:\Documents and Settings\21286\My Documents\MATLAB'
I'd like to be able to chop it off right before "\MATLAB" as it turns out there are more than one 's\' instances within the string.
  댓글 수: 1
Fangjun Jiang
Fangjun Jiang 2011년 6월 20일
You shall explain what is your need for the "chopping". Giving a path string and get the lowest folder name? If that is the case, fileparts() would be the best. This outloc is just an example, right? There are many ways to do it. For example:
regexprep(outloc,'.+\\','')

댓글을 달려면 로그인하십시오.


Gerd
Gerd 2011년 6월 20일
Hi William,
if you use
str = outloc(strfind(outloc,'\MATLAB'):end)
you will get
str = \MATLAB
Gerd

William
William 2011년 6월 21일
Here is my function:
function newstring = stringchopeer(stringinput, pat) % The purpose of this m file is to chop the ends off strings % the "pat" is the beginning of the area in which we would like to see % chopped
%Example: %pat = 'MATLAB'; %stringinput = 'D:\Documents and Settings\212068586\My Documents\MATLAB'
endpoint = regexp(stringinput, pat); newstring = []; for w = 1:length(stringinput) if w < endpoint newstring(w) = stringinput(w); end
end disp(char(newstring))
It works but I think it outputs ascii at the same time????
Thanks
Bill
  댓글 수: 1
Fangjun Jiang
Fangjun Jiang 2011년 6월 21일
Bill, would newstring=regexprep(stringinput,[pat,'.*'],'') work for your task?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by