Delete specific rows from character array
显示 更早的评论
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 个评论
Akira Agata
2017-8-12
Hi, I have just posted my answer on how to delete the first '*' and the last ';'. But to answer to your last question, I need to know what 'the long number of characters' exactly means. The 'character' in your question means only alphabet, or including numbers (0-9)? How many characters do you mean by the phrase 'long number of characters' ?
Sara Ghayouraneh
2017-8-12
Akira Agata
2017-8-14
编辑:Akira Agata
2017-8-14
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;'
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!