MATLAB: How to creat cell array

cell arraysrandom string

how to creat function which will have an input integer n. if n positive it will create and return a cell array with strings of random characters of increasing lengths from 1 to n
for example
functionname(4)
ans=
'z' 'zf' 'zfk' 'zfke'
and if it negative it will decreasing
many thanks

Best Answer

Your example only includes lower case so this code does as well.
function randLetters = myfunc(integer)
letters = 'a' : 'z';
rnum = randi(length(letters), 1, abs(integer));
randLetters = cell(1, abs(integer));
for i = 1:abs(integer)
randLetters{i} = letters(rnum(1:i));
end
if integer < 0
randLetters = fliplr(randLetters);
end
Example
myfunc(8)
ans =
1×8 cell array
{'r'} {'rw'} {'rwf'} {'rwfh'} {'rwfht'} {'rwfhtb'} {'rwfhtbt'} {'rwfhtbta'}
myfunc(-4)
ans =
1×4 cell array
{'whke'} {'whk'} {'wh'} {'w'}