How to remove delimiters, or special characters from a cell?
13 次查看(过去 30 天)
显示 更早的评论
I have this cell array which contains items followed by special characters used as delimiters: 160225MD0004;#2;#13161504900013;#1 I will first convert this cell to a char. But afterwards, I want to obtain the following on their own:
a = 160225MD0004
b = 13161504900013
Also how to make this general, for example-three items and more: 123345KR00994;#3;#160225MD0004;#2;#13161504900013;#1
I should obtain:
a = 123345KR00994
b = 160225MD0004
c = 13161504900013
Note : (;#3) and (;#) have different usages : first one always follows the items to state the order (;#3)(;#2) (;#1)..etc. The second one (#;)separates two items- notice that the last item 13161504900013 doesn't have a trailing (#;) because there is no adjacent item to separate it from but it is still followed by (;#1) to state order.
Note: the individual alphanumeric characters can be of any length- no standard length. I just need extract them to assign them to individual variables.
0 个评论
采纳的回答
Charles Dunn
2016-3-31
编辑:Charles Dunn
2016-3-31
This is a great time for regular expressions! The commands are very similar in bash scripting as well, so it's a useful tool to learn.
%input string
str = '123345KR00994;#3;#160225MD0004;#2;#13161504900013;#1';
%split string in to cell array by delimiter ';#'
t = regexp(str,';#','split');
%put alphanumeric values along first row, order along second row
t = reshape(t,2,length(t)/2);
alphanumeric = t(1,:);
%convert order to array of numbers
indices = str2double(t(2,:));
%order alphanumeric values properly (already ordered in example string...)
alphanumeric = alphanumeric(length(alphanumeric) - indices + 1);
更多回答(1 个)
Leonardo Wayne
2016-4-1
2 个评论
Charles Dunn
2016-4-4
the convert string is cut off on your screen shot, but are you sure it ends with ";#1"? my first guess would be that there is something strange happening at the end of the line. maybe there is an extra character at the end, like a space or a new line? not sure...
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!