How can I request an output in real time without interrupting my program?

6 views (last 30 days)
I am running a optimization program that can take several hours to converge. I would like to be able to type a command in the command window (such as how typing Ctrl+C causes a function to stop running) and be able to get an immediate output, but not interrupt my program to do so.
I do not want to have the function prompt the user or anything like that, because I would like it run smoothly for the most part when I am not asking for this output.
Is this possible with MATLAB?
Thanks,
-Jeremy

Answers (2)

Jan
Jan on 2 Nov 2012
No, this is not possible in Matlab.
You have to modify the optimization program to store intermediate values in a location, where they can be accessed externally. But even then your Matlab main thread is blocked by the optimization routine. Therefore you have to open another Matlab session to control the intermediate results. A bigh problem remains the kind of storage: Writing the intermediate data to a file is not trivial, because reading this file might be conflicting with writing. Example: The intermediate status is a [1 x 2] double value. Then you read the first scalar, the optimization function writes a new status. Afterwards the 2nd value is read. Consequence: You could see an infeasible point as intermediate value - not useful...
But this is a common problem. To be exact such common, that all optimization function I know personally have an output of intermediate values already. They write a log file and append new values, such that an incomplete writing of the status can be detected easily, because the last line is not complete.

Alan Weiss
Alan Weiss on 2 Nov 2012
There are ways to do this via an output function. You could, for example, store whatever intermediate results or data you like in a variable, and have the output function monitor for a keypress. If the user presses a key, or a particular key, you could have the variable stored to a file or the workspace, and stop the optimization.
For examples of output functions, see this example or this example. For more details on output functions, see this section.
Alan Weiss
MATLAB mathematical toolbox documentation

Categories

Find more on Get Started with Optimization Toolbox 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!