Updating guidata between multiple callbacks with one button in Matlab GUI

1 次查看(过去 30 天)
Hello!
I'm having problem with updating guidata inbetween callbacks resulting in the error: "Reference to non-existent field 'outflow'."
Here is my code, it would be awesome if someone could point out where I'm doing something wrong! Or atleast the parts that I believe are relevant.
function pushbutton1_Callback(hObject, eventdata, handles)
y = str2double(handles.years);
y = y*365;
years_use = num2str(y);
Q_out = handles.outflow;
set_param(bdroot,'StopTime', years_use);
set_param([bdroot '/Q'], 'Value', Q_out);
sim('River.slx')
The first edit text box where I want to get information from:
function edit1_Callback(hObject, eventdata, handles)
handles.years = get(hObject,'String');
guidata(hObject,handles);
And the second one:
function edit2_Callback(hObject, eventdata, handles)
handles.outflow = get(hObject,'String');
guidata(hObject,handles);
So the problem is, as stated in the first paragraph, that the outflow doesn't get updated as a field. While years does get through. And I don't understand why.
Thank you in advance for your answers!
Regards, Niklas
  1 个评论
Niklas
Niklas 2015-6-30
The problem was solved by adding handles.years and handles.outflow to the opening function. And manually entering the values into the edit textboxs, not trying to run it with the preset value from the GUIDE.

请先登录,再进行评论。

回答(1 个)

Geoff Hayes
Geoff Hayes 2015-6-30
Niklas - I copied and pasted your above code into a simple GUI and did not notice any problems with the callbacks firing properly. So either the above is incomplete or you have perhaps missed a step.
Are you sure that you have entered in data for both the years (in edit1) and the outflow (in edit2) before pressing the push button? Because that is one way that may lead to this error.
You may want to put a breakpoint in the edit2_Callback and verify that it is being called after you have entered data in this control and then give focus to some other control (which should cause the callback to fire).
  1 个评论
Niklas
Niklas 2015-6-30
Thank you for your time!
The problem was solved by adding handles.years and handles.outflow to the opening function. And manually entering the values into the edit textboxs, not trying to run it with the preset value from the GUIDE.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by