How to redirect matlab to previous section of coding?

7 views (last 30 days)
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
Danial Logan
Danial Logan on 1 Dec 2014
the condition of this problem is everything must be in one script. if it was up to me I would use several function m-files.

Sign in to comment.

Accepted Answer

Guillaume
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
Danial Logan
Danial Logan on 1 Dec 2014
thanks for the excellent answer. regarding your last point. could you give me a little more guidance as to how i would go about doing the unmaintainable way
Guillaume
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

Sign in to comment.

More Answers (0)

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!