creating callback for each string of a popupmenu in GUI

11 Ansichten (letzte 30 Tage)
sermet
sermet am 5 Jul. 2014
Beantwortet: Image Analyst am 5 Jul. 2014
%I have a popupmenu in GUI and I created 3 strings (x,y,z) in this popupmenu. I need to create callback for each 3 strings. Each string has different codes so when user click one of them, related popupmenu's codes need to work.

Akzeptierte Antwort

Image Analyst
Image Analyst am 5 Jul. 2014
Try this:
function popupmenu1_Callback(hObject, eventdata, handles)
try
% Get value of popup
selectedIndex = get(handles.popupmenu1, 'value');
% Take action based upon selection
if selectedIndex == 1
results = Function1();
elseif selectedIndex == 2;
results = Function2();
elseif selectedIndex == 3;
results = Function3();
end
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end

Weitere Antworten (1)

Robert Cumming
Robert Cumming am 5 Jul. 2014
Create a single callback for the popupmenu then switch inside that callback based on which string is selected, i.e.
function popupCallback ( obj, event )
str = get ( obj, 'string' );
val = get ( obj, 'value' );
switch str{val}
case 'str1'
callbackItem1 ();
case 'str2'
callbackItem2 ();
case 'str3'
callbackItem3 ();
end
end
  1 Kommentar
sermet
sermet am 5 Jul. 2014
Robert, this is my function from created popupmenu in GUI;
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array % contents{get(hObject,'Value')} returns selected item from popupmenu1 % function popupCallback ( obj, event )
%when I implement your codes into this function, it gives error;
"Undefined function or variable 'obj'"

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