Togglebutton Problem
5 ビュー (過去 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 で Migrate GUIDE Apps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!