deleting part of a string
17 次查看(过去 30 天)
显示 更早的评论
I have a string that I only want part of how do I get rid of the part I do not need.
for example:
original new
dogcat_1_12334 ==>dogcat
dogcat_2_13342 ==>dogcat
dogcat_3_13442 ==>dogcat
0 个评论
回答(3 个)
Jan
2013-7-17
s = 'dogcat_1_12334';
t = strtok(s, '_');
1 个评论
arvind ramasamy
2018-7-11
I make a serial communication with an arduino and get the data of accelerometer mag and gyro values my data comes in as a string. 's' is the object i created for serial communication
data =fscanf(s,%s) % gives me the data from the arduino
i want my vector size to be 500. so when I run it in for loop
for i= 1:50
data =fscanf(s,'%s');
end
my output is: data = initializationsuccesful!
data = ax:123ay:23az:1234gx:23gy:50gz:43mx:23my:123mz:234
and goes on.
I use,
A_x(:,i) = str2num(data(:,4:8))
to get my vector. but since 'initializationsuccessful!' is not a number i am unable to form my vector. so how can I do it ???? this is my question
Evan
2013-7-17
编辑:Evan
2013-7-17
Here's another answer, just in case the characters you want to keep don't fall in sequential order. If they don't, regexp with a "match" argument will split them up into separate cells. regexprep however, will just remove the unwanted parts.
ex1 = 'dogcat_1_12334'
ex2 = 'dog_1_12334_cat'
>> regexprep(ex1,'[^a-z]','')
ans =
dogcat
>> regexprep(ex2,'[^a-z]','')
ans =
dogcat
To modify this for more general cases, just change the string argument '[^a-z]'. Put any characters than you want to keep from being removed in the brackets []. The carat ^ operator specifies that "everything but" what is present in the brackets is modified to the second string argument, ''. Since '' is blank, that means anything not in the [^] will be removed.
0 个评论
Azzi Abdelmalek
2013-7-17
编辑:Azzi Abdelmalek
2013-7-17
original='dogcat_1_12334'
new=regexp(str,'[a-z]+','match']
1 个评论
Azzi Abdelmalek
2013-7-17
in case you have numbers and characters
new=regexp(str,'[a-z0-9]+','match','once')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!