for loop for switch case

3 次查看(过去 30 天)
Qiana Curcuru
Qiana Curcuru 2020-3-10
回答: BobH 2020-3-10
I have a switch case:
switch test
case test1
x=user_defined_value
a=x+1
case test2
x=user_defined_value
a=x+2
end
I want the user to be able to iterate through one case several times. For example, x=[1,2,3,4] for case 'test2' like a forloop. How would I do this?

采纳的回答

BobH
BobH 2020-3-10
would arrayfun work for your code? It would handle both x as a single number and x as a vector, setting 'a' to a single number or a vector to match the input
x = 1;
arrayfun(@(P) P+2, x)
ans =
3
x = [1 2 3 4];
arrayfun(@(P) P+2, x)
ans =
3 4 5 6

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by