Delete specific rows from character array
Mostra commenti meno recenti
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 Commenti
Akira Agata
il 12 Ago 2017
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
il 12 Ago 2017
Akira Agata
il 14 Ago 2017
Modificato: Akira Agata
il 14 Ago 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;'
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Characters and Strings in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!