How to redirect matlab to previous section of coding?
7 views (last 30 days)
Show older comments
basically i have a piece of coding which displays different tasks. the user gets to select which task they wish to perform. the last option allows them to pick all of them. how do i essentially tell matlab to go back and perform all these task one by one.
so what i have is: TASK 1 TASK 2 TASK 3 TASK 4 TASK 5 and TASK 6 which does all the above
how do i get matlab to run through parts of the previous coding when the user selects TASK 6. also, i don't want any separate m-files. everything must be in one script.
thank you, really appreciate any help
2 Comments
Accepted Answer
Guillaume
on 30 Nov 2014
The proper way, and the most simple: Have each task as a function in its own file. You may not like this solution but that's what you should do. Write simple self-contained functions that are easy to read, debug and understand
Slightly messier: Convert your script as a function, the tasks as private subfunctions of your main function. They can all be in the same file. Downside: not so easy to find each subfunction in a long file.
The unmaintanable way: Use a while loop in your script, and some switch statement for each task. Result: a big mess of spaghetti code, difficult to read, to debug and more importantly, impossible to reorganise.
Bottom line: use functions, that's what they're for.
2 Comments
Guillaume
on 2 Dec 2014
Actually, for the unmaintanable way, I would go with a for loop iterating over an ordered list of tasks to run rather than a while loop:
%build list of tasks to be run any way you want:
taskstorun = [1 2 3 4 3 3 1]; %for e.g.
for task = taskstorun
switch task
case 1
%do task 1
case 2
%do task 2
...
end
end
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!