symmetrical character array with only letters

Im trying to make a function that gives a logical true if the character array is symmetrical and logical false if not.
So far I have:
a = 'abba'
isItSym = isequal(a(:),flip(a(:)))
res = isItSym
But for anything that is not A-Z or a-z it needs to come back as logical false and it does not (ex: 'p##p').
Im thinking some sort of if function with an sprintf to split the array up and then find any false values with char(0:64) and so on. Im a little new with matlab tho so im having trouble goin about this. Thanks!

 채택된 답변

Voss
Voss 2022년 2월 20일
편집: Voss 2022년 2월 20일
Use isstrprop():
a = 'abba';
isItSym = isequal(a(:),flip(a(:))) && all(isstrprop(a(:),'alpha'))
isItSym = logical
1
a = 'p##p';
isItSym = isequal(a(:),flip(a(:))) && all(isstrprop(a(:),'alpha'))
isItSym = logical
0

댓글 수: 5

Elena
Elena 2022년 2월 20일
thank you!
Voss
Voss 2022년 2월 20일
You're welcome!
Elena
Elena 2022년 2월 22일
follow up: how can i say that if the input is empty (ie a = ' ' ) that it's also a logical 0?
Elena
Elena 2022년 2월 22일
is was thinking
if myStr == '';
res = 0
else res = isItSym
but it doesnt seem to be working?
DGM
DGM 2022년 2월 22일
편집: DGM 2022년 2월 22일
a = 'abba';
isItSym = ~isempty(a) && isequal(a(:),flip(a(:))) && all(isstrprop(a(:),'alpha'))
isItSym = logical
1
a = 'abbc';
isItSym = ~isempty(a) && isequal(a(:),flip(a(:))) && all(isstrprop(a(:),'alpha'))
isItSym = logical
0
a = 'a##a';
isItSym = ~isempty(a) && isequal(a(:),flip(a(:))) && all(isstrprop(a(:),'alpha'))
isItSym = logical
0
a = '';
isItSym = ~isempty(a) && isequal(a(:),flip(a(:))) && all(isstrprop(a(:),'alpha'))
isItSym = logical
0

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

질문:

2022년 2월 20일

편집:

DGM
2022년 2월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by