Delete specific rows from character array

9 views (last 30 days)
I have this matrix:
A =
'*8da9b9e39909762fb8044bfc9b90;'
'*5da9b9e30ba870;'
'*5da9b9e30ba875;'
'*8da9b9e39909762fb8044bfc9b90;'
'*8da9b9e358ab00090ed69ae2d795;'
'*5da9b9e30ba870;'
'*5da9b9e30ba87f;'
'*5da9b9e30ba87f;'
'*5da9b9e30ba876;'
'*8da9b9e39909762fb8044bfc9b90;'
'*5da9b9e30ba876;'
'*5da9b9e30ba876;'
'*5da9b9e30ba876;'
'*5da9b9e30ba877;'
'*5da9b9e30ba877;'
'*5da9b9e30ba877;'
'*5da9b9e30ba877;'
'*5da9b9e30ba877;'
'*8da9b9e358ab07a2d55cf4e40e32;'
'*8da9b9e39909762fb8044bfc9b90;'
I want to write a code to delete the asterisk(*) and semicolon(;) as the first and last characters of each row. I also want to define a new matrix in which I desire to only have the rows with the long number of characters, e.g. '*8da9b9e39909762fb8044bfc9b90;'. I appreciate your help.
  3 Comments
Sara Ghayouraneh
Sara Ghayouraneh on 12 Aug 2017
Akira,
Thank you so much for the answer. As you can see it is a matrix with multiple rows and one column. What I need to do is to keep the rows with 28 number of chars (most of them starts with 8) and delete the other ones (mostly starting with 5) and then put them into a new matrix. For example if we consider the first 5 rows, I want to keep the 1st, 4th, and 5th rows and get rid of 2nd and 3rd rows and put the three desired rows into a new 3*1 matrix.
Akira Agata
Akira Agata on 14 Aug 2017
Edited: Akira Agata on 14 Aug 2017
OK. Then, you can also use regular expression to identify such rows. For example, the following code finds and extracts the string which starts with '*' followed by 28 or more characters (a-z,A-Z,0-9 and '_') and ends with ';'.
A ={'*8da9b9e39909762fb8044bfc9b90;',...
'*5da9b9e30ba870;',...
'*5da9b9e30ba875;',...
'*8da9b9e39909762fb8044bfc9b90;',...
'*8da9b9e358ab00090ed69ae2d795;',...
'*5da9b9e30ba870;',...
'*5da9b9e30ba87f;',...
'*5da9b9e30ba87f;',...
'*5da9b9e30ba876;',...
'*8da9b9e39909762fb8044bfc9b90;',...
'*5da9b9e30ba876;',...
'*5da9b9e30ba876;',...
'*5da9b9e30ba876;',...
'*5da9b9e30ba877;',...
'*5da9b9e30ba877;',...
'*5da9b9e30ba877;',...
'*5da9b9e30ba877;',...
'*5da9b9e30ba877;',...
'*8da9b9e358ab07a2d55cf4e40e32;',...
'*8da9b9e39909762fb8044bfc9b90;'};
% Find start index of string which starts with '*' followed by 28 or more
% characters (a-z,A-Z,0-9 and '_') and ends with ';'
startIndex = regexp(A,'^\*\w{28,}\;$');
% Find non-empty cell
idx = ~cellfun(@isempty, startIndex);
% Extract the selected cells
Output = A(idx);
The 'Output' is the following cell array.
'*8da9b9e39909762fb8044bfc9b90;'
'*8da9b9e39909762fb8044bfc9b90;'
'*8da9b9e358ab00090ed69ae2d795;'
'*8da9b9e39909762fb8044bfc9b90;'
'*8da9b9e358ab07a2d55cf4e40e32;'
'*8da9b9e39909762fb8044bfc9b90;'

Sign in to comment.

Accepted Answer

Akira Agata
Akira Agata on 12 Aug 2017
Using the regular expression, it can be done easily, like:
A ={'*8da9b9e39909762fb8044bfc9b90;',...
'*5da9b9e30ba870;',...
'*5da9b9e30ba875;',...
'*8da9b9e39909762fb8044bfc9b90;',...
'*8da9b9e358ab00090ed69ae2d795;',...
'*5da9b9e30ba870;',...
'*5da9b9e30ba87f;',...
'*5da9b9e30ba87f;',...
'*5da9b9e30ba876;',...
'*8da9b9e39909762fb8044bfc9b90;',...
'*5da9b9e30ba876;',...
'*5da9b9e30ba876;',...
'*5da9b9e30ba876;',...
'*5da9b9e30ba877;',...
'*5da9b9e30ba877;',...
'*5da9b9e30ba877;',...
'*5da9b9e30ba877;',...
'*5da9b9e30ba877;',...
'*8da9b9e358ab07a2d55cf4e40e32;',...
'*8da9b9e39909762fb8044bfc9b90;'};
% Delete first '*' character
A1 = regexprep(A,'^\*','');
% Delete last ';' character
A2 = regexprep(A,'\;$','');

More Answers (0)

Community Treasure Hunt

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

Start Hunting!