MATLAB只在开关情况块中运行第一种情况



我正试图将csv文件中的数据分离为"块";然后我把这些数据分为10个不同的类别。每个区块的顶部都有一组空格。每个类别包含660个区块。目前,我的代码成功地放入了第一个块,但只有第一个块。不过,它确实正确地计算了块的数量。我不明白为什么它只在块计数正确的情况下放入第一个块,如果有任何帮助,我将不胜感激。csv文件可以从这里下载。https://archive.ics.uci.edu/ml/machine-learning-databases/00195/

fid = fopen('Train_Arabic_Digit.txt','rt');
traindata = textscan(fid, '%f%f%f%f%f%f%f%f%f%f%f%f%f', 'MultipleDelimsAsOne',true, 'Delimiter','[;', 'HeaderLines',1);
fclose(fid);
% Each line in Train_Arabic_Digit.txt or Test_Arabic_Digit.txt represents 
% 13 MFCCs coefficients in the increasing order separated by spaces. This 
% corresponds to one analysis frame. Lines are organized into blocks, which
% are a set of 4-93 lines separated by blank lines and corresponds to a 
% single speech utterance of an spoken Arabic digit with 4-93 frames.
% Each spoken digit is a set of consecutive blocks.
% TO DO: how get blocks...split? with /n?
% In Train_Arabic_Digit.txt there are 660 blocks for each spoken digit. The
% first 330 blocks represent male speakers and the second 330 blocks 
% represent the female speakers. Blocks 1-660 represent the spoken digit 
% "0" (10 utterances of /0/ from 66 speakers), blocks 661-1320 represent
% the spoken digit "1" (10 utterances of /1/ from the same 66 speakers 
% 33 males and 33 females), and so on up to digit 9.
content = fileread( 'Train_Arabic_Digit.txt' ) ;
default = regexp(content,'n','split');
digit0=[];
digit1=[];
digit2=[];
digit3=[];
digit4=[];
digit5=[];
digit6=[];
digit7=[];
digit8=[];
digit9=[];
blockcount=0;
a=0;
for i=1:1:length(default)
if strcmp(default{i},'            ')
blockcount=blockcount+1;
else
switch blockcount % currently only works for blockcount=1 even though
%it does pick up the number of blocks...
case blockcount>0 && blockcount<=660 %why does it not recognize 2 as being<660
a=a+1;
digit0=[digit0 newline default{i}];
case blockcount>660 && blockcount<=1320
digit1=[digit1 newline default{i}];
case blockcount<=1980 && blockcount>1320
digit2=[digit2 newline default{i}];
case blockcount<=2640 && blockcount>1980
digit3=[digit3 newline default{i}];
case blockcount<=3300 && blockcount>2640
digit4=[digit4 newline default{i}];
case blockcount<=3960 && blockcount>3300
digit5=[digit5 newline default{i}];
case blockcount<=4620 && blockcount>3960
digit6=[digit6 newline default{i}];
case blockcount<=5280 && blockcount>4620
digit7=[digit7 newline default{i}];
case blockcount<=5940 && blockcount>5280
digit8=[digit8 newline default{i}];
case blockcount<=6600 && blockcount>5940
digit9=[digit9 newline default{i}];
end
end
end

这是因为您以某种方式混淆了if-else语法和switch-case语法。请注意,像blockcount>0 && blockcount<=660这样的表达式总是返回一个逻辑值,这意味着它要么是0,要么是1。现在,当blockcount等于1时,第一个大小写表达式也得到1,其余结果为0,因此,1==1,第一个块运行。但是当blockcount变为2时,第一个大小写表达式仍然得到1和2~=1,所以什么都没发生!

您可以使用if-else,也可以将大小写表达式更改为包含值范围的单元格数组。根据文件:

开关块测试每个案例,直到其中一个案例表达式是的。以下情况为真:

  • 对于数字,case_expression==switch_expression。

  • 对于字符向量,strcmp(case_expression,switch_expression(==1。

  • 对于支持eq函数的对象,case_expression==switch_expression。重载eq函数的输出必须为逻辑值或可转换为逻辑值。

  • 对于单元格数组case_expression单元数组匹配switch_expression(如上面针对数字所定义的(,字符矢量和对象

它应该类似于:

switch blockcount
case num2cell(0:660)
digit0 ...
case num2cell(661:1320)
digit1 ...
...
end

但是,这段代码需要很长时间才能完成。首先,在循环中始终避免a = [a b]。调整矩阵大小非常耗时。始终预先分配a并执行a(i) = b

相关内容

  • 没有找到相关文章

最新更新