MATLAB Answers

Delete specific rows from character array

32 views (last 30 days)
Sara Ghayouraneh
Sara Ghayouraneh on 12 Aug 2017
Edited: Akira Agata on 14 Aug 2017
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
Akira Agata
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!