matlab 中 switch 的例子
发布网友
发布时间:2022-04-29 21:51
我来回答
共1个回答
热心网友
时间:2023-10-09 11:38
我这里有个更加简单的,你可以参考一下:
method = 'Bilinear';
switch lower(method)
case {'linear','bilinear'}
disp('Method is linear')
case 'cubic'
disp('Method is cubic')
case 'nearest'
disp('Method is nearest')
otherwise
disp('Unknown method.')
end
已经经过测试,可以运行!
我的QQ382101365
热心网友
时间:2023-10-09 11:39
Programming in MATLAB: Switch/Case Statement - Code
x = 3.0; % numeric variable
units = 'mm'; % string variable
switch units
case {'in','inch'} % multiple matches
y = 2.54*x; % converts to centimeters
disp ([num2str(x) ' ' units ' converted to cm is :' num2str(y)])
% disp is used to print pretty in the command window
% in the above a string vector is being printed
case {'m','meter'}
y = x*100; % converts to centimeters
disp ([num2str(x) ' ' units ' converted to cm is :' num2str(y)])
case { 'millimeter','mm'}
y = x/10;
disp ([num2str(x) ' ' units ' converted to cm is :' num2str(y)])
case {'cm','centimeter'}
y = x;
disp ([num2str(x) ' ' units ' converted to cm is :' num2str(y)])
otherwise
disp (['unknown units:' units])
y = nan; % not a number
end
热心网友
时间:2023-10-09 11:39
help switch
有详细的案例
遇到问题多多help