Hi guys, I have a function which will accept the characters and n, this function is a cell array with strings of decreasing lengths, from the integer n to 1. For example, when I type buildstr('a',4), the answer should be 'abcd' 'abc' 'ab' 'a'. However the code below doesnt satisfy my requirement. Can someone point out the error code?
function str=buildstr(ch,n) temp=''; if(n<0) str=''; else str=cell(n,1); for i=n:1 if(i==1) str(i)={char(ch)}; else temp=char(str(i+1)); str(i)={strcat(temp,char(ch))}; end end end
Best Answer