How to convert characters array into integer number array
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Azzi Abdelmalek
2014-4-9
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 个评论
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
更多回答(1 个)
Walter Roberson
2014-4-9
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!