我可以在Switch case Matlab函数中使用输入字符串数组吗?

时间:2013-06-25 02:32:38

标签: string matlab switch-statement

我尝试通过为我的项目使用字符串数据来进行和弦检测,并且我像这样编写代码

function akor=NoteAkor(notes)
notes=notes(3,:)
switch notes
   case notes={'A','C#/Db','E'}
      akor = 'Chord A mayor';
   case notes={'B' 'D' 'F#/Gb'}
      akor = 'Chord B';
    case notes={'C' 'E' 'G'}
        akor = 'Chord C mayor'
    case notes={'D' 'F#/Gb' 'A'}
        akor = 'Chord D mayor'
    case notes={'E' 'G#/Ab' 'B'}
        akor = 'Chord E mayor'
    case notes={'F' 'A' 'C'}
        akor = 'Chord F mayor'
    case notes={'G' 'A#/Bb' 'D'}
        akor = 'Chord G mayor'
    case notes={'A' 'C' 'E'}
        akor = 'Chord A minor'
    case notes={'B' 'D' 'F#/Gb'}
        akor = 'Chord B minor'
    case notes={'C' 'D#/Eb' 'G'}
        akor = 'Chord C minor'
    case notes={'D' 'F' 'A'}
        akor = 'Chord D minor'
    case notes={'E' 'G' 'B'}
        akor = 'Chord E mayor'
    case notes={'F' 'G#/Ab' 'C'}
        akor = 'Chord F mayor'
    case notes={'G' 'A#/Bb' 'D'}
        akor = 'Chord  G mayor'
   % put all other patters in similar case
   otherwise
      akor = '';
      error('not detected');
end
fprintf( 1, '%s\n', akor );

但错误:文件:NoteAkor.m行:4列:14 等号左侧的表达式不是作业的有效目标。

任何人都可以帮我修改我的代码或者给我另一种方法来使这段代码有效吗? Thx之前...

1 个答案:

答案 0 :(得分:0)

switch构造的语法错误:您只需在notes之后提及变量名称(switch)一次。请勿在{{1​​}}关键字后添加:

case