is there a command in matlab for waiting
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Walter Roberson
le 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.
Plus de réponses (4)
Rick Rosson
le 9 Sep 2011
if isempty(result)
...
else
...
end
4 commentaires
Walter Roberson
le 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
le 9 Sep 2011
Alternatively:
if exist('result','var')
...
else
...
end
HTH.
Rick
0 commentaires
Voir également
Catégories
En savoir plus sur Entering Commands dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!