String 我可以在开关箱Matlab函数中使用输入字符串数组吗?
我试图通过在我的项目中使用字符串数据来进行和弦检测,我编写了如下代码String 我可以在开关箱Matlab函数中使用输入字符串数组吗?,string,matlab,switch-statement,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'}
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 );
但它的错误是:File:NoteAkor.m行:4列:14
等号左侧的表达式不是有效的赋值目标
任何人都可以帮助我修复我的代码,或者给我另一种方法使代码正常工作?
Thx before…您的
开关
构造语法错误:您只需要在开关
之后提及变量名(注释
)一次。不要将其添加到案例
关键字之后:
switch notes
case {'A','C#/Db','E'}
akor = 'Chord A mayor';
case {'B' 'D' 'F#/Gb'}
akor = 'Chord B';
...
otherwise
akor = '';
error('not detected');
end
我给你的代码有什么问题吗?那里的语法是正确的。