Pass command to engEvalString() and return immediately
显示 更早的评论
When using engEvalString from my programs it is not always necessary to wait for the MATLAB console to send back a response (i.e. the prompt '>>' ). Is it possible to tell the engine to execute my commands asynchronously?
Example: My program emits "plot(x,y);" when a window is already open but has to wait seconds before the plot finishes and the MATLAB prompt returns.
An experiment with engOutputBuffer, setting the buffer size to 0, didn't work as hoped. It is understandable that returning immediately might be a bad idea when the application keeps sending commands without pause but maybe there is special non- blocking engine call that I can try?
It is of course possible to setup a parallel thread in my code to do the waiting but it feels like overkill.
3 个评论
I'm not sure there is anything other than another thread to not block the executing thread when it is time to refresh the image. However, rather than calling the high level plot() function again on an existing plot, you might find setting the XData, YData arrays directly more responsive -- but, then again, maybe not if it's actually redrawing the image that is the bottleneck.
marcel hendrix
2026-3-16,18:53
I think that will all depend upon how the COM server is configured internally...it will do what it is told to do, however long that might take. If it knows the graphics are being updated and the command prompt doesn't come back until after in an interactive session, I don't think it will behave any differently to the caller from afar...the internal engine doesn't know any different from the two.
MATLAB doesn't implement an asynch COM server model -- here's a blog from MS on what one would do if were writing one for background info.
AI Bot returned the following...
If you need non-blocking behavior, you must handle the asynchrony at the client level or use a different MATLAB interface:
- Client-Side Threading: Run the COM calls on a background thread within your client application (e.g., using Task.Run in C# or a worker thread in C++). This keeps your UI responsive while the background thread waits for the synchronous COM call to return.
- MATLAB Engine for Python/C++: Modern alternatives like the MATLAB Engine API explicitly support asynchronous calls (e.g., using the background=True argument) and return "future" objects.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 COM Component Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!