MATLAB: Function to capitalize first letter in each word in string but forces all other letters to be lowercase

functionlive scriptMATLABuppercase

Does anyone know how to create a function which accepts a string and capitalizes the first letter in each word of the string, but also forces the other letters to be lowercase?
Any advice would be greatly appreciated!!
This is my attempt so far:
str=['this is a TEST'];
for i=1:length(str);
if str(1,i(1));
str= upper(str);
else str(1,i);
str= lower(str);
end
end

Best Answer

str='this is a tEST';
str=lower(str)
idx=regexp([' ' str],'(?<=\s+)\S','start')-1
str(idx)=upper(str(idx))