How to convert characters array into integer number array

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.

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 9 日

0 投票

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 件のコメント

Gova ReDDy
Gova ReDDy 2014 年 4 月 9 日
I tried your above posted code but don't know why it is error like this "Undefined function or variable 'str'.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 9 日
str is your string
Gova ReDDy
Gova ReDDy 2014 年 4 月 9 日
編集済み: Gova ReDDy 2014 年 4 月 9 日
Thanks and but I tried to read my txt file as shown here
fid = fopen('../data/change.txt');
F = fread(fid);
butthe result 'F' is of size 519x1 but the original txt file contains only 173 alphabet characters. Can you help me out in reading the file properly.
The txt file is attached here.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 9 日
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 2014 年 4 月 9 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2014 年 4 月 9 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by