Return character array with increasing size based on input
Show older comments
I'm trying to make a cell-array that increases in length and has the previous characters in the following cells + the next letter in the alphabet
the inputs are supposed to be a starting letter and a number that indicates the amounts of cells(and then also the amount of letters in the last cell)
k = input('write startingletter and then number:')
k = a3
g = str2num(k(2:end))
inncell{1,1} = k(1);
inncell{1,2} = g;
outcell = makecell(inncell) %the function makecell
outcell=
{'a'} {'ab'} {'abc'}...
this is the output I need
and this is what I've come up with, but it's not working
% a = 97, z = 122 ASCII
k = input('gimme one letter then one pos. number(exs: a5):','s');
g = str2num(k(2:end));
while isletter(k(1))~= 1 || isempty(g) || g <= 0
%errorcheck if invalid input
disp('----ERROR! input ONE letter THEN ONE POSITIVE number');
k = input('gimme one letter then one pos. number(exs: a5):','s');
g = str2num(k(2:end));
end
inncell{1,1} = k(1);
inncell{1,2} = g;
outcell = makecell(inncell);
function outcell = makecell(inncell)
n = inncell{2};
outcell = cell(1,n);
for i = 1:n
for k = 1:i
if k == 1
outcell{1, i} = char(double(inncell{1}));
else
outcell{1, i} = strcat(char(double(inncell{1}), char(double(inncell{1})+ k-1)));
end
end
end
end
3 Comments
Walter Roberson
on 23 Mar 2019
There is a one line solution once you know the starting letter and length ;-)
Nikolai
on 23 Mar 2019
Walter Roberson
on 23 Mar 2019
It is not one function, it is a single expression.
Hint:
10 11 12 13 14
10 11 12 13 14
10 11 12 13 14
10 11 12 13 14
10 11 12 13 14
-->
10 0 0 0 0
10 11 0 0 0
10 11 12 0 0
10 11 12 13 0
10 11 12 13 14
looks like a triangle of entries.
Accepted Answer
More Answers (0)
Categories
Find more on Data Preprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!