Effacer les filtres
Effacer les filtres

How to convert characters array into integer number array

2 vues (au cours des 30 derniers jours)
Gova ReDDy
Gova ReDDy le 9 Avr 2014
Commenté : Gova ReDDy le 9 Avr 2014
Hello,
I have a text file in which all the contents are characters like a,b,c,d…z . Can somone explain how to convert theses alphabets in txt file into numbers like a corresponds to1, b corresponds to 2 ,in the converted file.
Thanks.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 9 Avr 2014
str='I have a text file in which all the contents are characters like a,b,c,d…z . Can somone explain how to convert theses alphabets in txt file into numbers like a corresponds to1, b corresponds to 2 ,in the converted file'
s='a':'z'
for k=1:numel(s)
str=regexprep(str,s(k),num2str(k),'ignorecase');
end
str
  5 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 9 Avr 2014
fid = fopen('change.txt');
str= textscan(fid,'%s');
fclose(fid)
str=str{:}
s='a':'z'
for k=1:numel(s)
str=regexprep(str,s(k),num2str(k),'ignorecase');
end
str
Gova ReDDy
Gova ReDDy le 9 Avr 2014
Thanks a lot.
It is working but I got a last doubt of how to plot the str- cellarray I looked at different matlab posts but was unable to make it plot.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 9 Avr 2014
converted_to_numbers = characters - 'a' + 1;
Then it is just a matter of writing out the numbers as text, which you already know how to do.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by