How to fix uicontrol callback error and retrieve the status of a pushbutton from a callback function ?
36 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Kinjal Mutha
am 9 Aug. 2019
Kommentiert: Kinjal Mutha
am 9 Aug. 2019
I have a GUI where I am using a pushbutton which says start test. The callback function for this pushbutton is in the gui code. I want the funtion to return a binary value indicating whether the button is pressed or not.
- If it is pressed it should display a message and set it back to 0 for the next time it will be pressed.
- The above point of resetting or overwriting, I have incorporated in my main matlab code and the gui one expecting atleast one to work
However when I run the main code it shows me an error at that line of code where I try to compare the the status of the pushbutton to 1 or 0
And when i run the gui code it shows me "Error while evaluating uicontrol Callback", "Error in ==> @(hObject,eventdata)
How can I solve this error? I have tried using handles and hObject but maybe i am not using them right.
Can someone help me just to display the status of the pushbutton?
function PbStartTest_Callback(hObject,eventdata, handles)
guidata(hObject,handles);
button_status = []
button_status == get((button_status.handles,'Value',str2num()));
if button_status == 1
display('test started');
button_status = 0;
end
end
This above code is my gui code and below I am mentioning the error part of my main code
if (GuiElements.PbStartTest.Value ~= 0) % showing error for this line
%Immediately reset the start button
GuiElements.PbStartTestValue = 0;
% After this point it should continue performing the selected test
end
0 Kommentare
Akzeptierte Antwort
Dennis
am 9 Aug. 2019
There are two different types of buttons in MATLAB, push buttons and toggle buttons. The latter actually have the function you desire build-in and i recommened to check if you want to use those instead.
uicontrol('style','togglebutton','callback',@myCallback)
function myCallback(hObj,~)
val=get(hObj,'Value')
end
Now it is hard to debug your code, because parts of the error message and parts of your code are missing. Thus i will only give you a few hints:
button_status = []
This line does nothing for you. Even worse if you planned to store your button value in this variable you overwrite it in every callback.
button_status == get((button_status.handles,'Value',str2num()));
This line has several errors in it and i am not completly sure what it is supposed to do. First you use '==' instead of '=' so this line checks for equality. In the line before you declared button_status as [] and the Value of a button is always 0, or 1 (in case of toggle buttons). So this line is supposed to disp(0) ?
Next button_status.handles does not exist, even if it exists somewhere in your main script you would overwrite it. Maybe you mean handles.button_status, but i can not know that. And i can not tell if handles.button_status exists somewhere in your code.
In the last part of that line you call str2num without any input, it might make sense to use it on the result of get, if your value could actually be a string.
if button_status == 1
display('test started');
button_status = 0;
end
button_status is still [] ...
if (GuiElements.PbStartTest.Value ~= 0) % showing error for this line
%Immediately reset the start button
GuiElements.PbStartTestValue = 0;
% After this point it should continue performing the selected test
end
In case GuiElements.PbStartTest is the handle to your button this would not work with pushbuttons (check toggle buttons or use a variable to store the state of your button).
Other than that you missed a dot:
GuiElements.PbStartTestValue = 0;
% ^.
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!