Togglebutton Problem
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2012-2-5
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 个评论
Walter Roberson
2012-2-15
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.
更多回答(4 个)
Steve
2012-2-15
1 个评论
Walter Roberson
2012-2-16
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
2012-2-16
1 个评论
Walter Roberson
2012-2-16
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
2012-2-16
1 个评论
Walter Roberson
2012-2-16
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!