msgbox and then do nothing.

13 Ansichten (letzte 30 Tage)
Nam Pham
Nam Pham am 5 Nov. 2015
Bearbeitet: Nam Pham am 6 Nov. 2015
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) ']']);

Akzeptierte Antwort

Guillaume
Guillaume am 5 Nov. 2015
Bearbeitet: Guillaume am 5 Nov. 2015
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 Kommentare
Guillaume
Guillaume am 6 Nov. 2015
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 am 6 Nov. 2015
Bearbeitet: Nam Pham am 6 Nov. 2015
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?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by