Togglebutton Problem
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am trying to use a toggle button in a project that I am working on and I am having trouble with the button it seems. I would like to execute certain code as long as the button is depressed (or the state is 1). However, it seems that the code goes into an infinite while loop. When I tried to check the state of the button as I pressed it, only 1's appeared and teh state did not change. Here is the simple code taht I am working with:
while Button == get(hObject,'Max')
EXECUTE CODE
if get(hObject,'Min')
break;
end
end
Any help is greatly appreciated!
0 Commenti
Risposta accettata
Walter Roberson
il 5 Feb 2012
You have to get() the Value of hObject. The Max and Min values do not change (not unless you specifically change them.)
Also, be sure that something in your code gives an opportunity for the GUI to react to events.
bmax = get(hObject, 'Max');
while get(hObject, 'Value') == bmax
EXECUTE CODE
drawnow(); %allow events
end
3 Commenti
Walter Roberson
il 15 Feb 2012
I would need to see the current code and the full traceback of the error message. There is no inherent reason a pushbutton cannot open and read a serial object.
Più risposte (4)
Steve
il 15 Feb 2012
1 Commento
Walter Roberson
il 16 Feb 2012
Out(i) = fread(s)
Unless you set the buffer size to 1, or you are _sure_ that the only thing being transmitted is the terminator character (with nothing before it), fread() is going to return multiple characters and you are going to try to store that vector in to the single array element Out(i)
If you are wanting to read a line, consider fgetl() and storing in Out{i}
On the other hand, you have not defined the variable s within the scope of that function, so referencing s is going to fail before you read anything. None of the code you showed configured the port or opens it :(
Please also remember to use drawnow() or pause() to give an opportunity for the GUI to update the button state.
Steve
il 16 Feb 2012
1 Commento
Walter Roberson
il 16 Feb 2012
Could you show the complete code for that routine, and also show the exact error messages being produced?
Have you put in a breakpoint and single-stepped through the close-down portion?
Steve
il 16 Feb 2012
1 Commento
Walter Roberson
il 16 Feb 2012
It just says there is an error there, but doesn't say what the error is?? Is it saying there was a timeout?
Is this on the first execution (i=1) of the first "while" ?
Note: in the loop you deltect Min and you fclose and delete and clear the object and break out of the loop. And then once the loop is broken out of, you again detect min and fclose the object and delete it and clear it, even though it is already gone.
Remember that the callback will be executed when the button is toggled back. In that case, it will not be Max so it will not create s, and it will not be Max so it will not loop, but it will be Min so it will try to fclose/delete/clear an object that does not exist. That is going to cause problems.
Suggest you "persistent" s . If s is [] and you have Min, nothing to be done so return. If s is not [] and you have Max, somehow s is initialized so leave it initialized and proceed to read the values. If s is not [] and you have Min, close / delete s, and set the persistent s to []. If s is [] and you have Max, initialize s in to the persistent variable and proceed with reading.
But -- watch out because s can appear or disappear at every drawnow(). And I am thinking that _maybe_ it could also interrupt after the fread(), but I am not certain of that.
Vedere anche
Categorie
Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!