# MATLAB: Capitalize first letter in string array

## MATLAB: Capitalize first letter in string array

You can do it using conventional slicing. To get a capital of a letter I used `upper` function

``````for i=1:size(str,2)
str{i}(1)=upper(str{i}(1))
end
``````

I think the best solution is to use extractBefore and extractAfter:

``````upper(extractBefore(str,2)) + extractAfter(str,1);
``````

Here is a performance benchmark:

``````function profFunc

str = [house, stone, summer];

n = 1E5;

% My solution
tic;
for i = 1:n
str = upper(extractBefore(str,2)) + extractAfter(str,1);
end
toc;

% Mikhail Genkins solution
tic;
for i = 1:n
for i=1:size(str,2)
str{i}(1)=upper(str{i}(1));
end
end
toc;

% EdRs Solution
tic;
for i = 1:n
str = string(cellfun(@(x) [upper(x(1)) x(2:end)], str, UniformOutput, false));
end
toc
end

>> profFunc
Elapsed time is 0.121556 seconds.
Elapsed time is 1.034617 seconds.
Elapsed time is 10.319375 seconds.
``````

#### MATLAB: Capitalize first letter in string array

The following code will do what you want:

``````string(cellfun(@(x) [upper(x(1)) x(2:end)], str, UniformOutput, false))
``````

`cellfun` applies the anonymous function following it to the variable str.

The anonymous function just returns the concatenation of the upper case of the first element followed by the rest of the string.

`string` converts the cell array back to a string array.

Edited: to convert back to a string array as requested in the comments.