필터 지우기
필터 지우기

How to Compare value returned by regexp and another string ?

조회 수: 1 (최근 30일)
Zineb EL MORCHID
Zineb EL MORCHID 2016년 4월 3일
편집: per isakson 2016년 4월 3일
nameF='aa.txt';
cni =regexp (nameF, '.txt', 'split');
I want to compare cni with a String by using strcmp for exemple strcmp(cni,'aa') so I converted its type by using char(cni) since cni is cell but it still returns false even they have the same value. What am I missing here ?

채택된 답변

per isakson
per isakson 2016년 4월 3일
편집: per isakson 2016년 4월 3일
"What am I missing here?" &nbsp I guess you are missing that
cni =regexp (nameF, '.txt', 'split');
returns a &nbsp<1x2 cell> array and that char(cni) returns a &nbsp<2x2 char> matrix
>> nameF='aa.txt';
cni =regexp (nameF, '.txt', 'split')
cni =
'aa' ''
>> char( cni )
ans =
aa
>> double( char( cni ) )
ans =
97 97
32 32
>>
instead use
strcmp( cni{1}, 'aa' )
or if 'aa.txt' is a filename
>> [ ~, name ] = fileparts('aa.txt')
name =
aa
>> strcmp( name, 'aa' )
ans =
1

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 3일
편집: Azzi Abdelmalek 2016년 4월 3일
nameF='aa.txt';
cni =regexp (nameF, '.txt', 'split')
The result is
cni =
'aa' ''
then
strcmp(cni,'aa')
gives
1 0
What is the problem here?
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 3일
cni ={'aa' ''} is a 1x2 cell array
strcmp(cni,'aa') returns two result [1 0]; maybe what you to do is
any(strcmp(cni,'aa'))
Zineb EL MORCHID
Zineb EL MORCHID 2016년 4월 3일
Yes, Thank You sir .

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by