Callback Problem

9 views (last 30 days)
Pietro Rossi
Pietro Rossi on 17 Apr 2011
Hi everyone! I built a simple GUI with GUIDE and edited Callback function. The program is very simple: you can choose from popup-menù if copy A to B or B to A. The first time, it works correctly, (i.e A to B or B to A), but if you want to use it again, there is an error, but I don't know why. This is a short part of the program (if you want, i can send you .m and .figure files, my e-mail is pietropp11@gmail.com):
[...]
function edit1_Callback(hObject, eventdata, handles)
val = str2double(get(hObject, 'String'));
if isnan(val)
set(hObject, 'String', 0);
errordlg('Devi inserire un numero','Errore');
end
handles.edit1 = val;
guidata(hObject,handles)
[...]
function edit2_Callback(hObject, eventdata, handles)
val = str2double(get(hObject, 'String'));
if isnan(val)
set(hObject, 'String', 0);
errordlg('Devi inserire un numero','Errore');
end
handles.edit2 = val;
guidata(hObject,handles)
[...]
function pulsante_Callback(hObject, eventdata, handles)
popup = get(handles.popupmenu1, 'Value');
switch popup
case 1
cambio= handles.edit1;
set(handles.edit2, 'String', num2str(cambio));
case 2
cambio= handles.edit2;
set(handles.edit1, 'String', num2str(cambio));
end
[...]
I'm sorry if my English in not very well. Best Regards
Pietro Rossi

Accepted Answer

Pietro Rossi
Pietro Rossi on 18 Apr 2011
Thank you for your answer, but I don't know the solution. What should I do for a simple GUI, i.e., does sum between three numbers? I would two 3 boxes (a,b,c) for number input and 1 text box (d) for the sum, but the first and the second edit (a and b) have to be the same: so if you set a, b==a, and if you set b, a==b. The number you see in the box have to change too. (d==a+b+c)
Thanks a lot for your answer Best Regards
Pietro Rosi
  1 Comment
Jarrod Rivituso
Jarrod Rivituso on 19 Apr 2011
You can definitely do this. Lets say the handles are such that
handles.edit1 - corresponds to the "a" edit box
handles.edit2 - corresponds to the "b" edit box
handles.edit3 - corresponds to the "c" edit box
handles.edit4 - corresponds to the "d" edit box
So, for the callback of edit boxes 1-3, you would need to recalculate d and set its value
>> a = get(handles.edit1,'Value');
>> b = get(handles.edit2,'Value');
>> c = get(handles.edit3,'Value');
>> set(handles.edit4,'Value',str2num(a+b+c))
Additionally, for the callbacks of edit boxes 1 and 2, you need to ensure that a and b remain the same. You could do this by analyzing hObject (the first callback parameter) to see which uicontrol was clicked
if (hObject == handles.edit1)
%We know the first edit box has changed, so change b
set(handles.edit2,'Value',get(handles.edit1,'Value'));
end
if (hObject == handles.edit2)
%We know the first edit box has changed, so change b
set(handles.edit1,'Value',get(handles.edit2,'Value'));
end

Sign in to comment.

More Answers (1)

Jarrod Rivituso
Jarrod Rivituso on 17 Apr 2011
It is strange for me to see code such as
handles.edit1 = val;
in your callback, considering that val is a number you determined seemingly based on user input.
GUIDE starts you off with a handles structure that contains "handles" to each of the uicontrol objects in your GUI. These handles are just unique numbers that are used to identify which uicontrol you are referring to.
When you change the values of these, then the number can no longer be used as an identifier to the uicontrol.
  1 Comment
Pietro Rossi
Pietro Rossi on 19 Apr 2011
thanks a lot for your help!

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!