Finding the index of a string within an array of strings

209 Ansichten (letzte 30 Tage)
Micah Boarts
Micah Boarts am 4 Nov. 2018
Kommentiert: ajay kumar am 22 Jun. 2022
I have an array composed of strings '0' 'P' 'E' and 'M' I am trying to find the index of the string 'P' within the array but the current lines I am using
staPlaLoc = find(maGen == 'P');
(wherein staPlaLoc is the index of 'P' within the array maGen) returns the following
Error using subsindex Function 'subsindex' is not defined for values of class 'string'.
  1 Kommentar
ajay kumar
ajay kumar am 22 Jun. 2022
Maybe you can use this
if your index is found you will get index number otherwise empty array
arr = ["a" "b" "c" "d"];
index = find(ismember(arr,"a"))
index = 1
index = find(ismember(arr,"z"))
index = 1×0 empty double row vector

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 4 Nov. 2018
May be this is what you want?
find(strcmp({'0' 'P' 'E' 'M'},'P'))

Weitere Antworten (0)

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!

Translated by