Return character array with increasing size based on input
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Nikolai
am 23 Mär. 2019
Kommentiert: Walter Roberson
am 23 Mär. 2019
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 Kommentare
Walter Roberson
am 23 Mär. 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.
Akzeptierte Antwort
dpb
am 23 Mär. 2019
strt=input('Starting letter: ');
N=input('Number groups: ');
% do your error checking here to ensure a single starting character and integer length in reason
...
res=cell(1,N); % preallocate the cell array
res(1)=cellstr(strt); % initial single letter
for i=2:N
res(i)=cellstr(strt:char(strt+i-1));
end
"Brute force, deadahead!" solution of creating each substring independently rather than trying to catenate onto an existing cell...does take advantage of Matlab's ability to do arithmetic on char() arrays/variables that makes things easier to code.
Example:
>> strt='a';
>> N=4;
>> res=cell(1,N);
>> res(1)={strt};
>> for i=2:N,res(i)={strt:char(strt+i-1)};end
>> res
res =
1×4 cell array
{'a'} {'ab'} {'abc'} {'abcd'}
>>
2 Kommentare
dpb
am 23 Mär. 2019
"...could use the colon operator that way with letters"
Yes, a char() array is just an array of characters; they're just integers internally that the display system knows to map to the right displayable characters. But, they can be treated like numbers when convenient, such as here...just have to cast back to the char() type because
>> 'a'+0
ans =
97
>>
changes the type to double...
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!