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

3 次查看(过去 30 天)
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.
  2 个评论
Jose abel De la Fuente
编辑:Jose abel De la Fuente 2016-10-23
Hi.
I have one problem seemed, which does not allow me update the handles.
My problem start when I try using the uipushtool and the ClickedCallback. In the handle function I have tried to update the handle of this way:
uipushtool(tbh,'CData',imread('Complementos\Verde.png'),'Separator','off',...
'TooltipString','Your toggle tool',...
'HandleVisibility','off',...
'ClickedCallback',...
{@marcarPunto,handles, cmP,cnP});
function marcarPunto(hObject,event,handles,cmP,cnP)
[handles.pnSin(cnP,cmP),handles.pmSin(cnP,cmP)]=ginputc(1, 'Color', 'w');
guidata(hObject,handles);
Please I will grateful with someone who could help me.
In my example hanldes has two elements (pnSin and pmSin).
Walter Roberson
Walter Roberson 2016-10-23
Jose abel De la Fuente: you are writing the x and y outputs of ginputc to the same output location. The location is going to end up as just the y value.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2013-3-28
编辑:Jan 2013-3-28
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
  2 个评论
Luca Amerio
Luca Amerio 2013-3-28
编辑:Luca Amerio 2013-3-29
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
Jan 2016-10-26
The "handles" from the input arguments are the version of the struct during the creation of the figure. Therefore later changes of this struct might not be included. If you do not modify this variable or GUIDE updates it, there is no problem. But this migtht depend on the GUIDE version, therefore it is a good idea to get the current version of handles manually.
"handles" is simply a struct here. The name is misleading, because it can contain anything and not only handles. A handle is a kind of unique address for each GUI element.
While "handles = guidata(hObject)" reads the struct from the figure's application data, "guidata(hObject, handles)" writes the struct back. Inside the command guidata thuis happens:
% handles = guidata(hObject)
set(ancestor(hObject, 'figure'), 'ApplicationData', handles);
% guidata(hObject, handles)
handles = get(ancestor(hObject, 'figure'), 'ApplicationData');
For clarity I leave out, that guidata uses a field of the ApplicationData only.

请先登录,再进行评论。

更多回答(1 个)

Jing
Jing 2013-3-28
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 个评论
Jan
Jan 2013-3-28
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
Luca Amerio 2013-3-28
编辑:Luca Amerio 2013-3-28
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by