for loop for switch case

5 views (last 30 days)
Qiana Curcuru
Qiana Curcuru on 10 Mar 2020
Answered: BobH on 10 Mar 2020
I have a switch case:
switch test
case test1
case test2
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?

Accepted Answer

BobH on 10 Mar 2020
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 =
x = [1 2 3 4];
arrayfun(@(P) P+2, x)
ans =
3 4 5 6

More Answers (0)

Community Treasure Hunt

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

Start Hunting!