MATLAB: How to create a string with names that differ, without a loop

continous variablestringsvector

I want
D_N = {'K1'; 'K2'; '…' ;'K9' ; '…'};
until Kn is reached with n equal to a parameter of my program (for example 39).
I can chose to use:
for ii = 1:39
D_N{ii} = sprintf('K%d', ii);
end
for ii = 1:39
D_N{ii} = ['K' num2str(ii)];
end
But is there a way without using this stupid loop?

Best Answer

The fastest solution is to use the undocumented function sprintfc:
sprintfc('K%d',1:39)
As this is undocumented, use at your own risk. Otherwise arrayfun works, but feels like overkill:
arrayfun(@(n)sprintf('K%d',n),1:39,'uni',0)
As an alternative you could use the string data class and compose:
compose('K%d',1:39)