matlab swich-case 结构相同key值如何实现?

版权声明:本文为博主原创文章,如能帮助到各位,荣幸之至,欢迎转载。 https://blog.csdn.net/m0_37639589/article/details/80301501

matlab swich-case 结构不同key值相同操作如何实现?

这是一个小知识,matlab帮助文档中并未说明当 key 值为数值型时,不同数值要实现相同操作的表达方法。而我凭借帮助文档的蛛丝马迹得出了方法。

帮助文档中给出了 key 值为字符型时不同 key 值同样操作的范例:

x = [12 64 24];
plottype = 'pie3';

switch plottype
    case 'bar' 
        bar(x)
        title('Bar Graph')
    case {'pie','pie3'}
        pie3(x)
        title('Pie Chart')
    otherwise
        warning('Unexpected plot type. No plot created.')
end

据此我推断,当 key 值为数字型时,可以将数值型表示成 cell 型解决问题。

for i = 1:n
    temp = daymat(i,:);
    switch ndayN(i)
        case 1
            for t = 17:23
                temp(4) = t;
                ndaymat = [ndaymat;temp];
            end
        case num2cell(2:5)
            for t = [0:15 17:23]
                temp(4) = t;
                ndaymat = [ndaymat;temp];
            end
        case 6
            for t = 0:15
                temp(4) = t;
                ndaymat = [ndaymat;temp];
            end
    end
end

这段代码仅仅是示范,你们缺少数据是运行不了的,但方法我想你们一目了然。这里运用了 num2cell 函数将数值转换为 cell。

好好学习,天天向上。

猜你喜欢

转载自blog.csdn.net/m0_37639589/article/details/80301501