필터 지우기
필터 지우기

How to handle strings containing greek letters in the code

조회 수: 10 (최근 30일)
Zachary Klamer
Zachary Klamer 2017년 1월 6일
댓글: Zachary Klamer 2019년 6월 26일
Hi, in my code I need to "spellcheck" a string in an object to insure a uniform format. I use regexprep for this. One such change needed is to change the greek letters 'α' and 'β' to 'a' and 'b' respectively.
It appears that the command window and actual code handle these letters differently and I need a way to use greek letters in the regexprep.
Code:
classdef myObject < handle
properties
text
end
methods
function obj = myObject(input)
obj.text = input;
spellcheck(obj);
end
function spellcheck(obj)
obj.text = regexprep(obj.text,'β','b');
end
end
end
For example: objText = myObject('testβtest')
returns 'testβtest'
The greek letters in the code are changed to '?' but work fine in the command window.
For example: regexprep('testβtest','β','b')
returns 'testbtest'
Is there a workaround to this problem? It must be relatively efficient, I'm sure opening a file containing the greek letters and assigning them to variables would work but that would slow down the code too much.
Thanks for the help,
~Zach
  댓글 수: 2
Greg
Greg 2017년 1월 6일
Maybe it's just me, but I didn't read an actual problem in your question. You say your spellcheck example needs to change beta to b, then go on to show us that it does...
Zachary Klamer
Zachary Klamer 2017년 1월 6일
It works differently depending on if it is done in the command window or in the code. In the command window it works fine (example 2) but in the code it does not work (example 1).

댓글을 달려면 로그인하십시오.

채택된 답변

stewpend0us
stewpend0us 2019년 6월 26일
if you type double('β') in the command line it will return 946.
In your code instead of using 'β' use char(946).
  댓글 수: 1
Zachary Klamer
Zachary Klamer 2019년 6월 26일
Nice!
I ended up getting around it with: native2unicode([206,178],'UTF-8')
But this is much more straightforward. Its silly that this is even a problem but this fixes it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Labels and Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by