Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Function inside GUI don't save modifies to "handles"

Asked by Luca Amerio on 28 Mar 2013

Hi everybody, I'm trying to do my first GUI, but I'm having some problems. Excuse me if my question is a bit stupid.

When I call a function (located inside the same .m file of the main code) I'm not able to modify the handles structure from inside the function.

I've done a very easy code to explain it better:

function pushbutton1_Callback(hObject, eventdata, handles)
      handles.A=1;
      guidata(hObject, handles);
      modify_A(hObject, eventdata, handles)
      disp(num2str(handles.A))
%%%%%%%%%%%%
function modify_A(hObject, eventdata, handles)
      handles.A=100;
      guidata(hObject, handles);

How can i do it?

thank you very much.

0 Comments

Luca Amerio

Products

No products are associated with this question.

2 Answers

Answer by Jan Simon on 28 Mar 2013
Edited by Jan Simon on 28 Mar 2013
Accepted answer
function pushbutton1_Callback(hObject, eventdata, handles)
    handles.A = 1;
    guidata(hObject, handles);   % Store handles
    modify_A(hObject, eventdata, handles)  % Update handles
    handles = guidata(hObject);  % Get newest version of handles
    disp(num2str(handles.A))
%%%%%%%%%%%%
function modify_A(hObject, eventdata, handles)
    handles.A = 100;  % modify handles
    guidata(hObject, handles);  % Store updated handles

Or more direct:

function pushbutton1_Callback(hObject, eventdata, handles)
    handles   = guidata(hObject);  % Do not trust handles from input
    handles.A = 1;
    handles   = modify_A(hObject, eventdata, handles)  % Update handles
    disp(num2str(handles.A))
%%%%%%%%%%%%
function handles = modify_A(hObject, eventdata, handles)
    handles.A = 100;  % modify handles

1 Comment

Luca Amerio on 28 Mar 2013

Great, I tried the second version and it works very well. Just one question: what it means:

handles   = guidata(hObject);  % Do not trust handles from input

It seams to works also without it, but i can't understand what it is. Why i shouldn't trust the handles variable from the input? What is the difference between that one and

guidata(hObject, handles);  % Store updated handles

?

My incomprehension could come from the fact that I don't really know what handles and hObject are, in addition to how this kind of function (without output) really work. Can you suggest me a place where I can find a more detailed description of this stuffs?

Thank you anyway very much for your help

Jan Simon
Answer by Jing on 28 Mar 2013

That's because you didn't update A after modification! Before disp, you should get A back from the handles by "A=handles.A;".

3 Comments

Luca Amerio on 28 Mar 2013

I'm very sorry. The dummy code I showed you is wrong. The error is given with disp(num2str(handles.A)) It happens even if i add a guidata(...) after the modify_A call.

Jan Simon on 28 Mar 2013

Please do not post wrong code and mention this in a comment to an answer. Use the chance to edit the original question, because this would be less confusing for the readers. And when you want the assistence of voluntary helpers, any kind of confusion is a bad idea.

Luca Amerio on 28 Mar 2013

I repeat: i'm very sorry. I tested the dummy code before posting it, but then i decided to modify it a little bit to make it clearer. I didn't noticed that i forgot to modify that value.

I try to be as clear and precise I can when I ask for help, but sometimes I miss something.

Sorry

Jing

Contact us