Load methods:
str = 'the rain in Spain falls mainly on the plane' spaceInd = strfind(str, ' '); % assume a word is preceded by a space startWordInd = spaceInd+1; % words start 1 char after a space startWordInd = [1, startWordInd]; % manually add the first word capsStr = upper(str); newStr = str; newStr(startWordInd) = capsStr(startWordInd)
More elegant / complex - cell arrays, textscan and cellfun are very useful for this kind of thing:
str = 'the rain in Spain falls mainly on the plane' function newStr = capitals(str) words = textscan(str,'%s','delimiter',' '); % assume a word is preceded by a space words = words{1}; newWords = cellfun(@my_fun_that_capitalizes, words, 'UniformOutput', false); newStr = [newWords{:}]; function wOut = my_fun_that_capitalizes(wIn) wOut = [wIn ' ']; % add the space back that we used to split upon if numel(wIn)>1 wOut(1) = upper(wIn(1)); end end end
Nivag
source share