Trying to Program a Switch-Case Expression for a Pop-up Menu in a MATLAB GUI

Can someone tell me what's wrong with my syntax or any other errors you spot?
% --- Executes on selection change in popupmenu_filters.
function popupmenu_filters_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu_filters (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% determine which filter to apply by reading string in pop-up menu
str = get(hObject, 'String');
val = get(hObject, 'Value');
% set current filter to the user-selected filter
switch str{val};
case 'imdilate'
handles.current_filter = imdilate
case 'imerode'
handles.current_filter = imerode
case 'imopen'
handles.current_filter = imopen
case 'imclose'
handles.current_filter = imclose
case 'imtophat'
handles.current_filter = imtophat
case 'imbothat'
handles.current_filter = imbothat
case 'entropy filter'
handles.current_filter = entropyfilt
end
handles.image2 = handles.current_filter(handles.image);
handles.axes2(imshow(handles.image2));
% pushbutton_save handles structure
guidata(hObject, handles);

 채택된 답변

handles.current_filter = @imdilate;
Without the @ it is going to _ call_ imdilate with no arguments.

추가 답변 (2개)

So when I'm adding slider capability to the code, how do I call that within the program. Would it be:
handles.image2 = handles.current_filter(handles.image, [handles.slider1 handles.slider2];
or something like that?

댓글 수: 5

And I'm still getting error messages like...
Error using iptchecknargin (line 57)
Function IMDILATE expected at least 2 input arguments
but was called instead with 0 input arguments.
Error in imdilate (line 119)
iptchecknargin(2,4,nargin,mfilename);
Error in FESgui>popupmenu_filters_Callback (line 544)
handles.current_filter = @imdilate
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in FESgui (line 49)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)FESgui('popupmenu_filters_Callback',hObject,eventdata,guidata(hObject))
Error using waitfor
Error while evaluating uicontrol Callback
because it's not recognizing my input arguments. What is causing that issue?
What input arguments? You aren't passing any input arguments?
But @imdilate should only be creating a function handle, not calling imdilate.
What happens if you type
@imdilate
at the command prompt?
Wait... just how old is your MATLAB?
And using the @function_handle has worked so far. I just haven't been able to apply the changes from the slider to the function.

댓글을 달려면 로그인하십시오.

Sean de Wolski
Sean de Wolski 2012년 7월 19일
편집: Sean de Wolski 2012년 7월 19일
Rather than using switch I would package this as a cell array of function handles and then extract the value:
ops={@imdilate,@imerode,@imopen} %etc
handles.current_filter = ops{val};

댓글 수: 4

This still works for a pop-up menu? How does the program know what the user selected?
Your code had
val = get(hObject, 'Value');
so Sean used that.
Just make sure your ops structure is in the same order as the entries in your String property.
Now it's giving me this error:
Attempted to access handles.axes2(234.013); index must be a positive integer or logical.
Error in FESgui>popupmenu_filters_Callback (line 562)
handles.axes2(imshow(handles.image2));
Even though my handles structure contains handles for axes2
pushbutton_browse: 238.0118
axes2: 233.0125
axes1: 228.0066
uipanel5: 243.0062
uipanel4: 241.0061
uipanel3: 239.0079
uipanel2: 56.0078
It should just be:
imshow(handles.image2,'parent',handles.axes2);

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by