deleting part of a string
19 ビュー (過去 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!