Filter löschen
Filter löschen

Whats wrong with my code?

3 Ansichten (letzte 30 Tage)
Ju
Ju am 24 Feb. 2012
hello all, i have a problem.
i have 3 edit box ---> c_edit, p_edit, and result.
i have 1 pushbutton.
my code for pushbutton callback is:
ciph = str2num(get(handles.c_edit,'String'));
priv = str2num(get(handles.p_edit,'String'));
[line ciphSize] = size(ciph);
[line privSize] = size(priv);
global bin
for i=1:1:ciphSize
for j = privSize:1
if ciph(i) >= priv(j)
bin(j) = 1;
ciph(i) = ciph(i) - priv(j);
else
bin(j) = 0;
end
end
set(handles.result,'String',num2str(bin));
for example :
ciph = 6 8 2
priv = 2 6
so bin= 1 0 1 1 0 1
but the result edit box show nothing. Can anyone tell me whats wrong with this?
thanks a lot.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Feb. 2012
If c_edit or p_edit are not convertible to number, then the size could come out 0, causing you to not loop at all.
If they are convertible to number, then if privSize is greater than 1, your loop over j would have a colon operator with a higher end value than start value, which is defined to mean no looping.
If you want to loop backwards you need a negative increment, such as
for j = privSize : -1 : 1
  1 Kommentar
Ju
Ju am 26 Feb. 2012
thank you very much, it works now..

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by