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

 채택된 답변

Adam Danz
Adam Danz 2018년 11월 23일
편집: Adam Danz 2018년 11월 23일

0 개 추천

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'}

댓글 수: 4

Dezdi
Dezdi 2018년 11월 25일
the -4 doesn't work for me it said 0X0 double
do you know what is worng?
Adam Danz
Adam Danz 2018년 11월 25일
Try again. It works for me. If you get the error again,copy and paste the entire error message here.
Dezdi
Dezdi 2018년 11월 25일
it said
1X4 cell array
{0x0 double} {0x0 double} {0x0 double} {0x0 double}
I write you message as well here did you get it.
Adam Danz
Adam Danz 2018년 11월 25일
편집: Adam Danz 2018년 11월 25일
I just tried it again and it works with negative and positive inputs. Try copying my version of the code again and replacing whatever code you have. After I published my answer I updated it minutes later with a correction. Maybe you are working with the verion that doesn't have the correction.
If you wrote a personal message I probably didn't get it since I don't take PMs. But I'd be glad to help you here.
Also, you should edit your question and add in the example you removed so your question is complete and understood by others who read it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

질문:

2018년 11월 23일

편집:

2018년 11월 26일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by