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!