MATLAB:switch语句问题
MATLAB: switch statement problems
我正在尝试解析某个文件。
我的想法是一边使用转换句一边逐行阅读。
我需要比较第一个单词是否符合某些字符串。
log = fopen('my_file');
tline = fgetl(log);
while ischar(tline)
split = strsplit(tline,',');
switch split(1)
case 'str1'
%do something
case 'str2'
%do something else
end
end
我不断收到“SWITCH 表达式必须是标量或
字符串常量。
split 和 split(1) 的 Class 是 char。变量在while的每一次循环中都是常量。
怎么了?
strsplit
returns 字符串元胞数组。您需要使用大括号访问结果的内容:
switch split{1}
case 'str1'
%do something
case 'str2'
%do something else
end
我正在尝试解析某个文件。
我的想法是一边使用转换句一边逐行阅读。 我需要比较第一个单词是否符合某些字符串。
log = fopen('my_file');
tline = fgetl(log);
while ischar(tline)
split = strsplit(tline,',');
switch split(1)
case 'str1'
%do something
case 'str2'
%do something else
end
end
我不断收到“SWITCH 表达式必须是标量或 字符串常量。
split 和 split(1) 的Class 是 char。变量在while的每一次循环中都是常量。
怎么了?
strsplit
returns 字符串元胞数组。您需要使用大括号访问结果的内容:
switch split{1}
case 'str1'
%do something
case 'str2'
%do something else
end