matrix dimension must agree

2019-08-20 21:59发布

问题:

My dropdown list contains the ff. strings: Low Pass, High Pass, Band Pass, Stop Band. Whenever I choose the Low Pass, the error below shows. The code below works for the rest.

My goal is to make the edtCutoff2 and txtRange invisible when I choose Low Pass and High Pass but the code below works only for High Pass.

Error:

Error using  == 
Matrix dimensions must agree.

Error in untitled>popFreqResp_Callback (line 168)
if ((str == 'Stop Band') | (str == 'Band Pass') == 1)

Error in gui_mainfcn (line 96)
        feval(varargin{:});

Error in untitled (line 42)
    gui_mainfcn(gui_State, varargin{:});

Error in@(hObject,eventdata)untitled('popFreqResp_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating uicontrol Callback

Code Snippet

function popFreqResp_Callback(hObject, eventdata, handles)
list=get(handles.popFreqResp,'String');
str=list{get(handles.popFreqResp,'Value')};
if ((str == 'Stop Band') | (str == 'Band Pass') == 1)
    set(handles.edtCutoff2,'Visible','on');
    set(handles.txtRange,'Visible','on');
else
    set(handles.edtCutoff2,'Visible','off');
    set(handles.txtRange,'Visible','off');
end

回答1:

You shouldn't compare strings using '==', because it will throw the error you see if the strings are not the same length. Essentially '==' is comparing two matrices of type char - if they don't have the same length, '==' isn't defined. Since 'Low Pass' has a length of 8, and 'Band Pass' has a length of 9, you can't compare them in this manner.

Use strcmp instead. Or strcmpi if you don't care about case.