How to remove delimiters, or special characters from a cell?

22 ビュー (過去 30 日間)
Leonardo Wayne
Leonardo Wayne 2016 年 3 月 31 日
コメント済み: Leonardo Wayne 2016 年 4 月 5 日
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.


Charles Dunn
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
Leonardo Wayne 2016 年 3 月 31 日
Thanks that is really good. I have actually done the same thing but with one or two steps more since I have to convert from cell to char. I have a Followup question: Say now I want to create folders with the names of the different cells of the alphanumeric cell array but obviously a general number of cells again. Using for loop and mkdir function, how can this be done?


その他の回答 (1 件)

Leonardo Wayne
Leonardo Wayne 2016 年 4 月 1 日
Hi I a get an error when I incorporate your code to my m file. See below.
The indices variables is a 1x3 array with values 3 2 NaN.
Help explain or fix?
  2 件のコメント
Leonardo Wayne
Leonardo Wayne 2016 年 4 月 5 日
Thanks I have made mistake by adding a (') character at the start and end of the excel cell that I am reading from. That is the source of the error.


Community Treasure Hunt

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

Start Hunting!

Translated by