Effacer les filtres
Effacer les filtres

Whats wrong with my code?

3 vues (au cours des 30 derniers jours)
Ju
Ju le 24 Fév 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.

Réponse acceptée

Walter Roberson
Walter Roberson le 24 Fév 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 commentaire
Ju
Ju le 26 Fév 2012
thank you very much, it works now..

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by