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) ']']);

采纳的回答

Guillaume
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
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.
Nam Pham
Nam Pham 2015-11-6
编辑:Nam Pham 2015-11-6
Firstly, sorry for my bad English, and thank you very much. I am new to learn MATLAB. I have to creat a GUI that user input signal and output bitstream. Line coding as NRZ,HDB3, B8ZS.... why
if any(temp ~= 2 % temp ~= 0)
but not
if any(temp~=2 && temp ~=0)
And strsplit command doesn't work. can you write full code and test, I tried much time but it doesn't work
usermessage = '+ + 0 - - + + - - + -';
if ~all(ismember(strsplit(usermessage), {'+', '0'}))
msgbox('Only + and 0 are allowed');
return;
end
And here is my full code for Unipolar NRZ
function pushbutton1_Callback(hObject, eventdata, handles)
usermessage = get(handles.signal,'string');
usermessage=str2num(strrep(strrep(strrep(usermessage,'+','2'),'-','1'),'0','0'));
bitstream=zeros(1,length(usermessage));
n=200;
t=0:1/n:length(usermessage);
x=zeros(1,length(t));
%kiem tra loi
for i=1:length(usermessage)
if usermessage(i)~=2&&usermessage(i)~=0
msgbox('Only + and 0 are allowed');
return;
end
end
% ve song
for i=0:length(usermessage)-1
if usermessage(i+1)==2
x(i*n+1:(i+1)*n)=1;
elseif usermessage(i+1)==0
x(i*n+1:(i+1)*n)=0;
end
end
% xuat bit
for i=1:length(usermessage)
if usermessage(i)==2
bitstream(i)=1;
elseif usermessage(i)==0
bitstream(i)=0;
end
end
set(handles.bitstream,'String',num2str(bitstream));
plot(t,x,'LineWidth',3);
axis([0 t(end) -0.1 1.1]);
grid on;
title([' Bitstream: [' num2str(bitstream) ']']);
How do you feel my code? What I should edit?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by