Comparing Rows of Char Array to String
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi everyone,
I have an 8x3 char array that looks like the following
Wed
Wed
Wed
Wed
Wed
Wed
Wed
Sat
I need to compare it to a string to perform one action when the day is Wednesday (or Wed) and a different action when the day is Saturday (or Sat). For i = 1:8, a loop runs to put the output of this if statement into a numeric vector.
How can I go about properly comparing each row of the char array to a string ('Wed' or 'Sat')?
Thanks in advance!
0 Kommentare
Antworten (1)
Guillaume
am 11 Feb. 2015
c = ['Wed'; 'Wed'; 'Fri'; 'Wed'; 'Sat'];
[~, actionnumber] = ismember(c, ['Wed'; 'Sat'], 'rows')
4 Kommentare
Image Analyst
am 11 Feb. 2015
Even more robustness can be gained by casting the day to lower case if you don't really care about the case of the letters. Or you could use strcmpi(). If you don't care about case then it's most robust if your code works regardless if they capitalized the day or not.
if strcmp(lower(dayname(i, :)), 'wed')
if strcmpi(dayname(i, :), 'Wed')
switch lower(dayname(i, :))
case 'wed'
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!