Filter löschen
Filter löschen

How to use the same variable in different functions in GUI MATLAB

9 Ansichten (letzte 30 Tage)
Mech Stud
Mech Stud am 31 Aug. 2018
Kommentiert: Mech Stud am 31 Aug. 2018
I have a popup menu with the following code. There are two options as C1 and C2. If the user selects C1, I want to set the value as 10 and if the user selcts C2, I want to set the value as 20.
function pop_Callback(hObject, eventdata, handles)
contents = cellstr(get(hobject,'String'));
A = contents{get(hObject,'Value')};
if (strcmp(A,'C1'))
X = 10;
elseif (strcmp(A,'C2'))
X = 20;
end
set(handles.pop,X)
I want to use another function with a pushbutton and static text to display the answer, where the output is, Whatever the set value + 12.
function push_Callback(hObject, eventdata, handles)
inX = get(handles.pop,X);
out = inX;
set(handles.ans,'String',out)
However, I have some error in set and get function.

Antworten (1)

Stephen23
Stephen23 am 31 Aug. 2018
Bearbeitet: Stephen23 am 31 Aug. 2018
At the end of a callback handles is not stored automatically, or somehow passed to other callbacks/functions. You have to do this explicitly, by calling guidata at the end of the callback:
guidata(hObject,handles)
Only then can you get .pop in the other callback. See the examples in the documentation:
  1 Kommentar
Mech Stud
Mech Stud am 31 Aug. 2018
Thanks for the guidance. I read and edited my code as follows
contents = cellstr(get(hobject,'String'));
A = contents{get(hObject,'Value')}
data = guidata(object_handle);
if (strcmp(A,'C1'))
data = 10;
elseif (strcmp(A,'C2'))
data = 20;
end
guidata(object_handle,data)
set(handles.pop,data)
function push_Callback(hObject, eventdata, handles)
in = get(handles.pop,'String');
answ = in;
set(handles.ans,'String',answ)
However, The 'answ' is C1 and C2 (The 'Value' or the pull down menu option).

Melden Sie sich an, um zu kommentieren.

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