is there a command in matlab for waiting

12 次查看(过去 30 天)
its needed sometimes matlab do nothing and not going next command line, till a specified thing happens.for example:
clc
clear all
g=[];
plot(r,'YDataSource','r');ylim([0 y1]);Xlim([0 x1])%r is a defined matrix
-(now after plotting, i want make a matrix in 'result' name manually but since now this matrix isn't defined so i need matlab wait here till i defined it)
if 'rezult' not exist
wait
end
for i=1:10
g=[g;result(i,i+1)] %if didn't wait, errors for defining of 'result'
end

采纳的回答

Walter Roberson
Walter Roberson 2011-9-9
No, there is no way to what you have asked in MATLAB.
You can test whether the variable named "result" exists by checking
if exist('result','var')
However, there is no way for any code you put after that to detect that the "result" matrix has somehow been created under the condition "not when i push a key or others cases".
If the program is executing, there is no way to somehow create "result" from outside the program (as could happen if the program was stopped in the debugger and "result" was defined at the debugger command prompt.)
If the program is executing, then you can use input() to allow values to be input, but then you have to "push a key or other cases" (you have to do something) in order to create "result"
If the program is executing, then you can use keyboard() to turn over control to the user, potentially allowing them to "poof" the variable in to existence, but then the user would have to deliberately enter the command "return" in order to continue.
There is no "transporter beam" for variables that can cause them to come in to existence without something being done to make them.
The closest you can get would be to use a timer set to a random time, with a callback that created "result" in the base workspace. The problem with that is there is no way to notify your running routine that the variable has been created.
This is what you can do instead, in combination with the random timer to make it happen "not when i push a key or others cases":
waitfor(h,'PropertyName',PropertyValue) blocks of the caller from executing until the value of 'PropertyName' for the graphics object h changes to the specific value PropertyValue or h closes (is deleted). If the value of 'PropertyName' is already PropertyValue, waitfor returns immediately without processing any events.
Notice though that it has to be a property of a graphics object, not a plain variable, and since it would have to be an existing property you would not be able to use the property as the variable "result" and check to see whether it existed or not. But since we are imaging magic ways for setting variables without doing anything to set them, we could imagine that the magic could set the property to true to indicate that "result" has been created and it is time to proceed.
Frankly, if it were me, I would just create an appropriate input figure and waitfor() that figure to close, a configuration that admits that the user is going to be involved in the interaction, whether by pressing a "Done" button or by clicking on the X to close the input window.
  1 个评论
mohammad
mohammad 2011-9-9
so much thanks
you are right, this is absolutely right when program is stopped, its stopped for creating 'result' also !
really nice
i creates 'result' by analyzing manually a figure so after closing this figure, i wish to start program . Then i am going to use 'waitfor'
i appreciate you Walter for your accurate and detailed explanation.

请先登录,再进行评论。

更多回答(4 个)

Rick Rosson
Rick Rosson 2011-9-9
if isempty(result)
...
else
...
end
  4 个评论
Walter Roberson
Walter Roberson 2011-9-9
I really think you need to test that, Rick. I think you will find that instead MATLAB will error out for trying to use an undefined variable.
Perhaps you are being influenced by the fact that persistent variables and global variables will be empty matrices until some other value is assigned to them?
Rick Rosson
Rick Rosson 2011-9-9
Yes, you are right. This solution will not work.

请先登录,再进行评论。


Oleg Komarov
Oleg Komarov 2011-9-9
I suggest to use the input function to manually enter the matrix.

Rick Rosson
Rick Rosson 2011-9-9
  1 个评论
mohammad
mohammad 2011-9-9
thanks for replying
i need waiting be finished when 'result' matrix was defined. not when i push a key or others cases. i saw waitfor command too and this works when figure be closed. if there is no command for existing a matrix i use this

请先登录,再进行评论。


Rick Rosson
Rick Rosson 2011-9-9
Alternatively:
if exist('result','var')
...
else
...
end
HTH.
Rick

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by