is there a command in matlab for waiting

17 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 9 Sep 2011
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 Comment
mohammad
mohammad on 9 Sep 2011
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.

Sign in to comment.

More Answers (4)

Rick Rosson
Rick Rosson on 9 Sep 2011
if isempty(result)
...
else
...
end
  4 Comments
Walter Roberson
Walter Roberson on 9 Sep 2011
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 on 9 Sep 2011
Yes, you are right. This solution will not work.

Sign in to comment.


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

Rick Rosson
Rick Rosson on 9 Sep 2011
>> doc pause
  1 Comment
mohammad
mohammad on 9 Sep 2011
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

Sign in to comment.


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

Categories

Find more on Loops and Conditional Statements 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!