MATLAB: Capitalizing every word in a string

capitalizingregexprepstringupper

str='The bottom of the deep blue sea'
strN='The Bottom Of The Deep Blue Sea'
To convert the string 'str' to 'strN' is used:
regexprep(str,'(\<[a-z])','${upper($1)}')
Can someone please xeplain this? Another suggestion which did not work was:
regexprep(str,'(\<\w)','${upper($1)}')
Is there a simpler (it is ok if it is longer) way of doing this?

Best Answer

I like simple Matlab code:
m = [false, isspace(str)];
m = m(1:length(str));
str(m) = upper(str(m))
By the way, tested with: '', ' ', 'a', 'aA', ' a', ' a ', ' ab c', ' ab c ' .
Some timings:
str0='The bottom of the deep blue sea';
tic
for k = 1:1e4
str = str0;
m = [false, isspace(str)];
m = m(1:length(str));
str(m) = upper(str(m));
end
toc
tic
for k = 1:1e4
str = str0;
str = regexprep(str,'(\<[a-z])','${upper($1)}');
end
toc
0.17 seconds % Simple Matlab
3.24 seconds % Powerful REGEXP