how to save an array of letters in matlab ?

2 Ansichten (letzte 30 Tage)
tomer polsky
tomer polsky am 26 Jul. 2018
Kommentiert: tomer polsky am 26 Jul. 2018
If for example I have the English alphabet and I want to create an array of each 4 letter for exmaple : if x='abcdefghijklmnopqrstuvwxyz' then each 4 letter in my new word will be : y=afkpuz and this is my code :
clc;
clear all;
close all;
x='abcdefghijklmnopqrstuvwxyz'
array=zeros(1,length(x));
counter=0;
for i=1:5:length(x)
counter=counter+1
num2str(i)
array(counter)=(x(i))
end
but for some reason instead of getting an array of letters I get an array of numbers . How do I fix it ?

Akzeptierte Antwort

Stephen23
Stephen23 am 26 Jul. 2018
Bearbeitet: Stephen23 am 26 Jul. 2018
"but for some reason instead of getting an array of letters I get an array of numbers"
Yes, because you specified array to be numeric:
array=zeros(1,length(x)); % zeros -> numeric double
and each time you allocate anything to this array MATLAB will try its best to convert it to a double (so the character gets converted to its char value). If you really want a character array, then you need to specify this, e.g.:
array = char(zeros(...));
But using a loop is very inefficient anyway, it is much simpler to use indexing:
>> x = 'abcdefghijklmnopqrstuvwxyz';
>> y = x(1:5:end)
y = afkpuz
  1 Kommentar
tomer polsky
tomer polsky am 26 Jul. 2018
thank you very much friend ,you helped me a lot .

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by