MATLAB: How to find the number of letters in a string? Just letters (A-Z, a-z) characterstring How can I find the number of letters in a string? Just letters (A-Z, a-z)? Best Answer Easiest way:string = 'This is a string.';NrLtrs = length(regexpi(string, '[a-zA-Z]'));The number of letters only is returned in the ‘NrLtrs’ variable. Related SolutionsMATLAB: Identifying positions of a string within a text file Probably something like this:fid = fopen(FileName,'rt');filetext = fread(fid,'*char');fclose(fid);newtext = regexprep(filetext, '\$\$', 'Hello', 'once');newtext = regexprep(newtext, '\$\$', 'World', 'once');fid = fopen(NewFilename, 'wt');fwrite(fid, newtext, '*char');fclose(fid); MATLAB: Changing letters to other letters with regexprep You don't need regexprep, using ismember and indexing is simpler:>> str = 'a':'z'str = abcdefghijklmnopqrstuvwxyz>> old = 'bcd';>> new = 'abc';>> [X,Y] = ismember(str,old);>> str(X) = new(Y(X))str = aabcefghijklmnopqrstuvwxyzNote that this works efficiently for any characters, not just a limited subset. In order to use regexprep you would probably need to use a dynamic replacement expression. Related QuestionEdit line in text documentLetters in MatrixExport matched lines from two text filesReading greek letters!
Best Answer