MATLAB Answers

How to fix uicontrol callback error and retrieve the status of a pushbutton from a callback function ?

14 views (last 30 days)
Kinjal Mutha
Kinjal Mutha on 9 Aug 2019
Commented: Kinjal Mutha on 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.
  1. If it is pressed it should display a message and set it back to 0 for the next time it will be pressed.
  2. 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 Comments

Sign in to comment.

Accepted Answer

Dennis
Dennis on 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 Comment

Kinjal Mutha
Kinjal Mutha on 9 Aug 2019
Hi Dennis,
Thankyou for looking into my problem. As you suggested I switched to toggle button and it worked. Please excuse my mistakes in the code above I am still trying to understand how GUI and the functions work.
However even after switching to togglebutton my GUI code is running but the main code is still showing error
if (GuiElements.PbStartTest.Value ~= 0) % showing error for this line
GuiElements.PbStartTest.Value = 0;
% After this point it should continue performing the selected test
end
How do I send the value of toggle switch to the main code?
My GUI function looks like this right now
function PbStartTest_Callback(hObject,eventdata,handles)
val = get(hObject,'Value')
if(val == 1)
set(hObject,'String','End Test');
end
if(val == 0)
set(hObject,'String','Start Test');
end
uicontrol('style','togglebutton','callback',@myCallback)
end

Sign in to comment.

More Answers (0)

Sign in to answer this question.


Translated by