[Help] Lệnh if trong GUIDE của Matlap.

hix anh em ơi giúp mình cái này một tí, mình làm bài tập nhóm nhưng bị kẹt chỗ này tìm hiểu mãi mà không hiểu sao đoạn code này nó không chạy được như ý muốn, mình Debug nó thì thấy chỉ cần không thoả mãn điều kiện của lệnh if đầu tiên là nó thoát khỏi chương trình luôn, nó ko thèm xuống phần else >.<. Hix anh em cao thủ vào xem giúp mình nha.:confused:
function pb_com_Callback(hObject, eventdata, handles)
% hObject handle to pb_com (see GCBO)
%%
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
s = get(handles.edit_status,'String');
if (s == 'Disconnect')
set(handles.pb_com,'String','Disconnect');
set(handles.edit_status,'String','Connect');
else
set(handles.pb_com,'String','Connect');
set(handles.edit_status,'String','Disconnect');
end
 

tungbk

Cố Vấn CLB
Staff member
Explanation
The code compares two strings using either the equal to (==) or not equal to (~=) relational operator. MATLAB software returns an error at run time if the strings being compared are different lengths. Even if MATLAB does not return an error when the code runs, it is likely that this code is inefficient.
Suggested Action
Use the strcmp function instead of a relational operator to compare two strings.

Nôm na là khi dùng == so sánh 2 string mà khác chiều dài thì sẽ báo lỗi và cũng có vẻ là không hợp lí, hiệu quả cho lắm nên khuyên sửa lại dùng lệnh strcmp
Nên code của em sửa lại thành if strcmp(s,'Disconnect') là ok
 
hì dạ cảm ơn anh Tùng, hì tiện thể cho em hỏi lun xem cái đoạn tiếng anh kia ở đâu vậy anh.
 

tungbk

Cố Vấn CLB
Staff member
:) Không biết e dùng bản MATLAB mấy anh dùng bản 2010 thì khi so sánh == nó báo gạch đỏ ở dưới là nên sửa lại strcmp đi kèm với chú thích tiếng anh đó luôn.
 
Top