msgbox and then do nothing.
조회 수: 13 (최근 30일)
이전 댓글 표시
Hello I am writing a GUI program, input (+)and (0), if I input (-), I want it show error box, do nothing. In my code, If I input (-) it show error box but it still run plot. How do I have to do? Thank you!
function pushbutton1_Callback(hObject, eventdata, handles)
temp = get(handles.signal,'string');
temp=strrep(temp,'+',2);
temp=strrep(temp,'0',0);
temp=strrep(temp,'-',1);
for i=1:length(temp)
if temp(i)==1
msgbox(' Only + and 0');
break;
end
end
n=200;
t=0:1/n:length(temp);
x=zeros(1,length(t));
for i=0:length(temp)-1
if temp(i+1)==2
x(i*n+1:(i+1)*n)=1;
elseif temp(i+1)==0
x(i*n+1:(i+1)*n)=0;
end
end
plot(t,x,'LineWidth',3);
axis([0 t(end) -0.1 1.1]);
grid on;
title([' Bitstream: [' num2str(bitstream1) ']']);
댓글 수: 0
채택된 답변
Guillaume
2015년 11월 5일
편집: Guillaume
2015년 11월 5일
Replace break by return.
Note that you do not need the for loop, use any and vectorised comparison instead
%...
temp=strrep(temp,'-',1);
if any(temp == 1)
msgbox(' Only + and 0');
return
end
Your code is very fragile. What if the user enters '*'? You don't detect that.
In your previous question (which you seem to have abandoned) I showed a much more efficient and robust way of converting your input string.
댓글 수: 3
Guillaume
2015년 11월 6일
The proper syntax would be
if any(temp ~= 2 % temp ~= 0)
Note the & instead of && because it's a vector operation.
As per my answer to you previous question, a cleaner way of achieving your test is with:
usermessage = get(handles.signal,'string'); %temp is a terrible variable name
if ~all(ismember(strsplit(usermessage), {'+', '0'}))
msgbox('Only + and 0 are allowed');
return;
end
The best way to get help is to post question in this forums as you've done so far. That way you get feedback from multiple people. My contact details are not public on purpose.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!