让Matlab继续而不等待结果 [英] Let Matlab continue without waiting for a result
问题描述
我有以下问题:如何告诉Matlab它不应该等待函数的结果?除了线程,还有其他方法吗?
I have the following question: How do I tell Matlab that it should not wait for the results of a function? Is there a way other than threads?
我的问题:我有一个函数A,它每隔几秒钟就会被计时器调用一次.如果遇到特定事件,则在函数A内调用另一个函数B.函数B打开一个批处理文件. 我希望功能A继续运行而不必等待功能B结束.有没有办法轻松做到这一点?
My problem: I have a function A that is called by a Timer every few seconds. If a specific event is met, another function B is called inside function A. Function B opens a Batch File. I want function A to go on without waiting for function B to end. Is there a way to easily do it?
很抱歉,如果已经提出此问题,但找不到满意的答案.也请原谅我的英语不好.
I'm sorry if this question was already asked, but I couldn't find a satisfying answer. Please also excuse my bad english.
我要感谢所有回答的人.
I would like to thank everyone who answers for their help.
推荐答案
在函数B中,只需在行末使用&
调用批处理文件.
In your function B, just call the batch file with a &
at the end of the line.
例如:
!mybatch.bat &
这将在后台模式下运行文件mybatch.bat
,并在调用后立即将执行返回给Matlab.
This will run the file mybatch.bat
in background mode and will return execution to Matlab immediately after the call.
,或者如果您喜欢完整的表格,则:
or if you prefer the complete form:
[status, result] = system('mybatch.bat &')
但是在这种情况下它是没有用的,因为在后台系统调用mybatch
,所以result
变量始终为空,而status
始终为0
(是否已找到文件mybatch.bat
并执行与否)
But in this case it is a bit useless, since the system call mybatch
in the background, the result
variable is always empty and status
is always 0
(whether a file mybatch.bat
was found and executed or not)
edit:如果只是批处理文件执行使您的程序变慢,这是一个快速的技巧.
edit: That is the quick trick in case it is only the batch file execution which is slowing down your program.
如果在函数B中有更多的matlab指令,并且确实需要在不等待的情况下继续运行函数A,则必须使用函数B设置侦听器对象作为回调.然后在您的函数A中触发事件(这将激活侦听器并调用函数B).
If you have more matlab instructions in function B and you really need function A to go on without waiting, you will have to set up a listener object with function B as a callback. Then in your function A, trigger the event (which will activate the listener and call function B).
这篇关于让Matlab继续而不等待结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!