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!