The variables which were stored in handles gets deleted once the program control gets out of the function loop despite using guidata()

11 Ansichten (letzte 30 Tage)
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
num=processe(hObject, handles);
guidata(hObject, handles);
t = str2num(get(handles.text2,'String'));
var= num - t;
set(handles.text2, 'String', num2str(var));
function num=processe(hObject, handles)
num = str2double(get(handles.edit,'String'));
if handles.i==1
handles.max=num;
handles.min=num;
end
handles.i=handles.i+1
if handles.max<num
handles.max=num;
elseif handles.min>num
handles.min=num;
end
set(handles.edit,'string','0');
handles.y(handles.i)= num;
handles.i=(handles.i)+1;
guidata(hObject, handles);
Using breakpoints, I discovered that once the function processe() is finished being executed, and the program control goes back into the pushbutton_2 callback, the variables(not just the values inside them) disappears from handles despite using guidata().
handles =
struct with fields:
%During the execution of function processe
figure1: [1×1 Figure]
uipanel1: [1×1 Panel]
edit: [1×1 UIControl]
text2: [1×1 UIControl]
axes1: [1×1 Axes]
pushbutton5: [1×1 UIControl]
pushbutton4: [1×1 UIControl]
pushbutton3: [1×1 UIControl]
pushbutton2: [1×1 UIControl]
pushbutton1: [1×1 UIControl]
i: 2
output: [1×1 Figure]
y: []
max: -5
min: -5
%After the function processe() has finished executing
handles =
struct with fields:
figure1: [1×1 Figure]
uipanel1: [1×1 Panel]
edit: [1×1 UIControl]
text2: [1×1 UIControl]
axes1: [1×1 Axes]
pushbutton5: [1×1 UIControl]
pushbutton4: [1×1 UIControl]
pushbutton3: [1×1 UIControl]
pushbutton2: [1×1 UIControl]
pushbutton1: [1×1 UIControl]
i: 1
output: [1×1 Figure]
y: []
Any thoughts on the matter, Thanks?

Antworten (1)

Stephen23
Stephen23 am 25 Mai 2019
Bearbeitet: Stephen23 am 25 Mai 2019
You need to call guidata inside processe. In its current location, your first guidata does nothing.
num=processe(hObject, handles);
guidata(hObject, handles); % <- you need to move this INSIDE of PROCESSE !
"... the variables(not just the values inside them) disappears from handles despite using guidata(). "
Because the copy handles is local to that workspace. If you make changes to handles and then do not save it within that workspace then all of those changes will simply be discarded when that function returns.
Note that within the pushbutton2_Callback workspace the first time you call guidata absolutely nothing has changed within handles, so calling guidata at that point is entirely superfluous: you are simply replacing one handles structure with an exactly identical handles structure (and none of this has anything to do with whatever happens inside of processe).

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by