How to Compare value returned by regexp and another string ?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Zineb EL MORCHID
am 3 Apr. 2016
Bearbeitet: per isakson
am 3 Apr. 2016
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 ?
0 Kommentare
Akzeptierte Antwort
per isakson
am 3 Apr. 2016
Bearbeitet: per isakson
am 3 Apr. 2016
"What am I missing here?"   I guess you are missing that
cni =regexp (nameF, '.txt', 'split');
returns a  <1x2 cell> array and that char(cni) returns a  <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
Weitere Antworten (1)
Azzi Abdelmalek
am 3 Apr. 2016
Bearbeitet: Azzi Abdelmalek
am 3 Apr. 2016
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 Kommentare
Azzi Abdelmalek
am 3 Apr. 2016
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'))
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!