Thread Subject: repetitive counter (GUI)

Subject: repetitive counter (GUI)

From: ching l

Date: 13 Jul, 2009 20:38:01

Message: 1 of 14

handles = guidata(gcbo);

if(handles.count <= 2)

handles.count= handles.count+1;
handles.count


else
    
    set(hObject, 'enable', 'off') % switch off button when the limit reach


end

Hi there

I would like the unable/off the push button when the counter reach 2, however,
the counter goes from counter= 1, counter = 2, counter = 3, counter= 3

What is the reason counter =3 is repeated twice? Is there a way to avoid the last counter to repeat twice?

Any advice would be much appreciated.

Thanks.

Subject: repetitive counter (GUI)

From: Sven

Date: 13 Jul, 2009 20:53:01

Message: 2 of 14

Follow through your code:

If counter is 2 or lower, THEN increment "counter".
OTHERWISE (ie, if counter is 3 or higher), set enable to off.

Does your program work as you intend it if you increment counter before checking if it's 2 or lower?


"ching l" <chinglnc@hotmail.com> wrote in message <h3g5v9$6ep$1@fred.mathworks.com>...
> handles = guidata(gcbo);
>
> if(handles.count <= 2)
>
> handles.count= handles.count+1;
> handles.count
>
>
> else
>
> set(hObject, 'enable', 'off') % switch off button when the limit reach
>
>
> end
>
> Hi there
>
> I would like the unable/off the push button when the counter reach 2, however,
> the counter goes from counter= 1, counter = 2, counter = 3, counter= 3
>
> What is the reason counter =3 is repeated twice? Is there a way to avoid the last counter to repeat twice?
>
> Any advice would be much appreciated.
>
> Thanks.

Subject: repetitive counter (GUI)

From: ching l

Date: 13 Jul, 2009 21:05:18

Message: 3 of 14

"sort of"-

it does increase but it didn't stop at 2. It increases till 3 and repeat counter=3 again.

Any idea why?




"Sven" <sven.holcombe@gmail.deleteme.com> wrote in message <h3g6rd$58f$1@fred.mathworks.com>...
> Follow through your code:
>
> If counter is 2 or lower, THEN increment "counter".
> OTHERWISE (ie, if counter is 3 or higher), set enable to off.
>
> Does your program work as you intend it if you increment counter before checking if it's 2 or lower?
>
>
> "ching l" <chinglnc@hotmail.com> wrote in message <h3g5v9$6ep$1@fred.mathworks.com>...
> > handles = guidata(gcbo);
> >
> > if(handles.count <= 2)
> >
> > handles.count= handles.count+1;
> > handles.count
> >
> >
> > else
> >
> > set(hObject, 'enable', 'off') % switch off button when the limit reach
> >
> >
> > end
> >
> > Hi there
> >
> > I would like the unable/off the push button when the counter reach 2, however,
> > the counter goes from counter= 1, counter = 2, counter = 3, counter= 3
> >
> > What is the reason counter =3 is repeated twice? Is there a way to avoid the last counter to repeat twice?
> >
> > Any advice would be much appreciated.
> >
> > Thanks.

Subject: repetitive counter (GUI)

From: dpb

Date: 13 Jul, 2009 21:09:33

Message: 4 of 14

ching l wrote:
....
> it does increase but it didn't stop at 2. It increases till 3 and
> repeat counter=3 again.
>
> Any idea why?
>
...
>> "ching l" <chinglnc@hotmail.com> wrote in message
>> <h3g5v9$6ep$1@fred.mathworks.com>...
>>> handles = guidata(gcbo);
>>> if(handles.count <= 2)
>>> handles.count= handles.count+1; handles.count
>>> else
>>> set(hObject, 'enable', 'off') % switch off button
>>> end
...

[Top posting repaired...please don't do that; it makes following the
conversation much more difficult...]

 > it does increase but it didn't stop at 2. It increases till 3 and
 > repeat counter=3 again.
 >
 > Any idea why?

As it happens, yes... :)

What happens in your logic when enter the function w/ handles.count==2?

--

Subject: repetitive counter (GUI)

From: ching l

Date: 14 Jul, 2009 07:19:01

Message: 5 of 14

dpb <none@non.net> wrote in message <h3g8bd$lin$1@news.eternal-september.org>...
> ching l wrote:
> ....
> > it does increase but it didn't stop at 2. It increases till 3 and
> > repeat counter=3 again.
> >
> > Any idea why?
> >
> ...
> >> "ching l" <chinglnc@hotmail.com> wrote in message
> >> <h3g5v9$6ep$1@fred.mathworks.com>...
> >>> handles = guidata(gcbo);
> >>> if(handles.count <= 2)
> >>> handles.count= handles.count+1; handles.count
> >>> else
> >>> set(hObject, 'enable', 'off') % switch off button
> >>> end
> ...
>
> [Top posting repaired...please don't do that; it makes following the
> conversation much more difficult...]
>
> > it does increase but it didn't stop at 2. It increases till 3 and
> > repeat counter=3 again.
> >
> > Any idea why?
>
> As it happens, yes... :)
>
> What happens in your logic when enter the function w/ handles.count==2?
>

Guess what, the results is the same :-(

I'm not sure why...

Subject: repetitive counter (GUI)

From: dpb

Date: 14 Jul, 2009 08:34:20

Message: 6 of 14

ching l wrote:
> dpb <none@non.net> wrote in message <h3g8bd$lin$1@news.eternal-september.org>...
>> ching l wrote:
>> ....
>>> it does increase but it didn't stop at 2. It increases till 3 and
>>> repeat counter=3 again.
>>>
>>> Any idea why?
>>>
>> ...
>>>> "ching l" <chinglnc@hotmail.com> wrote in message
>>>> <h3g5v9$6ep$1@fred.mathworks.com>...
>>>>> handles = guidata(gcbo);
>>>>> if(handles.count <= 2)
>>>>> handles.count= handles.count+1; handles.count
>>>>> else
>>>>> set(hObject, 'enable', 'off') % switch off button
>>>>> end
>> ...
>>
>> [Top posting repaired...please don't do that; it makes following the
>> conversation much more difficult...]
>>
>> > it does increase but it didn't stop at 2. It increases till 3 and
>> > repeat counter=3 again.
>> >
>> > Any idea why?
>>
>> As it happens, yes... :)
>>
>> What happens in your logic when enter the function w/ handles.count==2?
>>
>
> Guess what, the results is the same :-(
>
> I'm not sure why...

Pretend you're the computer executing the code you provided. Work thru
line by line w/ what you've told it to do if you start w/ a given value
for count (specifically, 2).

--

Subject: repetitive counter (GUI)

From: ching l

Date: 14 Jul, 2009 13:10:39

Message: 7 of 14

dpb <none@non.net> wrote in message <h3hgfb$96h$1@news.eternal-september.org>...
> ching l wrote:
> > dpb <none@non.net> wrote in message <h3g8bd$lin$1@news.eternal-september.org>...
> >> ching l wrote:
> >> ....
> >>> it does increase but it didn't stop at 2. It increases till 3 and
> >>> repeat counter=3 again.
> >>>
> >>> Any idea why?
> >>>
> >> ...
> >>>> "ching l" <chinglnc@hotmail.com> wrote in message
> >>>> <h3g5v9$6ep$1@fred.mathworks.com>...
> >>>>> handles = guidata(gcbo);
> >>>>> if(handles.count <= 2)
> >>>>> handles.count= handles.count+1; handles.count
> >>>>> else
> >>>>> set(hObject, 'enable', 'off') % switch off button
> >>>>> end
> >> ...
> >>
> >> [Top posting repaired...please don't do that; it makes following the
> >> conversation much more difficult...]
> >>
> >> > it does increase but it didn't stop at 2. It increases till 3 and
> >> > repeat counter=3 again.
> >> >
> >> > Any idea why?
> >>
> >> As it happens, yes... :)
> >>
> >> What happens in your logic when enter the function w/ handles.count==2?
> >>
> >
> > Guess what, the results is the same :-(
> >
> > I'm not sure why...
>
> Pretend you're the computer executing the code you provided. Work thru
> line by line w/ what you've told it to do if you start w/ a given value
> for count (specifically, 2).
>
> --Thanks. I understand why it increases to 3 now, but I suppose it should jump to the "else" section when the counter reaches 3 and not repeating the counter again?

Counter= 3 occurs twice. Which part did I miss again?

Subject: repetitive counter (GUI)

From: dpb

Date: 14 Jul, 2009 13:26:28

Message: 8 of 14

ching l wrote:
> dpb <none@non.net> wrote in message <h3hgfb$96h$1@news.eternal-september.org>...
>> ching l wrote:
>>> dpb <none@non.net> wrote in message <h3g8bd$lin$1@news.eternal-september.org>...
>>>> ching l wrote:
>>>> ....
>>>>> it does increase but it didn't stop at 2. It increases till 3 and
>>>>> repeat counter=3 again.
>>>>>
>>>>> Any idea why?
>>>>>
>>>> ...
>>>>>> "ching l" <chinglnc@hotmail.com> wrote in message
>>>>>> <h3g5v9$6ep$1@fred.mathworks.com>...
>>>>>>> handles = guidata(gcbo);
>>>>>>> if(handles.count <= 2)
>>>>>>> handles.count= handles.count+1; handles.count
>>>>>>> else
>>>>>>> set(hObject, 'enable', 'off') % switch off button
>>>>>>> end
>>>> ...
>>>>
>>>> [Top posting repaired...please don't do that; it makes following the
>>>> conversation much more difficult...]
>>>>
>>>> > it does increase but it didn't stop at 2. It increases till 3 and
>>>> > repeat counter=3 again.
>>>> >
>>>> > Any idea why?
>>>>
>>>> As it happens, yes... :)
>>>>
>>>> What happens in your logic when enter the function w/ handles.count==2?
>>>>
>>> Guess what, the results is the same :-(
>>>
>>> I'm not sure why...
>> Pretend you're the computer executing the code you provided. Work thru
>> line by line w/ what you've told it to do if you start w/ a given value
>> for count (specifically, 2).
>>
>> --Thanks. I understand why it increases to 3 now, but I suppose it
should jump to the "else" section when the counter reaches 3 and not
repeating the counter again?
>
> Counter= 3 occurs twice. Which part did I miss again?

OK, I didn't look at the output; I thought the question was "why 3"?

Hmmm....that's not clear to me from the posted code, either.

Are you sure there isn't something not in this code that took one off it
or another line that outputs it on that condition?

The code in isolation does appear that the 2nd "3" shouldn't be displayed.

I gather this is somehow called from a GUI? I've never written a single
ML GUI interface (and hope to continue that record :) ) -- perhaps
there's something about events that there's another in the queue or
something related to the GUI portion; that's out of my realm...

--

Subject: repetitive counter (GUI)

From: Steven Lord

Date: 14 Jul, 2009 13:39:18

Message: 9 of 14


"dpb" <none@non.net> wrote in message
news:h3i1jj$r9o$1@news.eternal-september.org...
> ching l wrote:

*snip*

>>> --Thanks. I understand why it increases to 3 now, but I suppose it
> should jump to the "else" section when the counter reaches 3 and not
> repeating the counter again?
>>
>> Counter= 3 occurs twice. Which part did I miss again?
>
> OK, I didn't look at the output; I thought the question was "why 3"?
>
> Hmmm....that's not clear to me from the posted code, either.
>
> Are you sure there isn't something not in this code that took one off it
> or another line that outputs it on that condition?

I also suspect that there's a line of code later on in the function that
displays the counter variable, probably inserted to try to diagnose another
problem (or perhaps to try to diagnose this problem.)

--
Steve Lord
slord@mathworks.com

Subject: repetitive counter (GUI)

From: ching l

Date: 14 Jul, 2009 21:32:01

Message: 10 of 14

"Steven Lord" <slord@mathworks.com> wrote in message <h3i1oe$q5s$1@fred.mathworks.com>...
>
> "dpb" <none@non.net> wrote in message
> news:h3i1jj$r9o$1@news.eternal-september.org...
> > ching l wrote:
>
> *snip*
>
> >>> --Thanks. I understand why it increases to 3 now, but I suppose it
> > should jump to the "else" section when the counter reaches 3 and not
> > repeating the counter again?
> >>
> >> Counter= 3 occurs twice. Which part did I miss again?
> >
> > OK, I didn't look at the output; I thought the question was "why 3"?
> >
> > Hmmm....that's not clear to me from the posted code, either.
> >
> > Are you sure there isn't something not in this code that took one off it
> > or another line that outputs it on that condition?
>
> I also suspect that there's a line of code later on in the function that
> displays the counter variable, probably inserted to try to diagnose another
> problem (or perhaps to try to diagnose this problem.)
>
> --
> Steve Lord
> slord@mathworks.com
>

OK. The whole thing is like this.

handles = guidata(gcbo);

if(handles.count<=2)

handles.count= handles.count+1;

handles.count= count

else

set(hObject, 'enable', 'off')

end

guidata(gcbo, handles);

[audio, fs] = wavread('C:\a.wav');
samples{300} = {audio, fs};

[audio, fs] = wavread('C:\b.wav');
samples{301} = {audio, fs};

[audio, fs] = wavread('C:\b.wav');
samples{302} = {audio, fs};

handles.randValue=299+handles.count;

play= handles.randvalue

wavplay(samples{handles.randValue}{:}) ;
 
guidata(hObject, handles);

--------------------
The results are

count= 0 play= 300 ;
count= 1 play= 301 ;
count = 2 play = 302 ;
play= 302 ; disable button

----------------------

what I want is
count= 0 play= 300 ;
count= 1 play= 301 ;
count = 2 play = 302 ;
disable button

can anyone tell me whether my logic is not right or the codes are not right?

Subject: repetitive counter (GUI)

From: dpb

Date: 14 Jul, 2009 23:46:59

Message: 11 of 14

ching l wrote:
...
> handles = guidata(gcbo);
> if(handles.count<=2)
> handles.count= handles.count+1;
> handles.count= count
> else
> set(hObject, 'enable', 'off')
> end
> guidata(gcbo, handles);
> ...
> handles.randValue=299+handles.count;
> play= handles.randvalue
> ...
>
> --------------------
> The results are
>
> count= 0 play= 300 ;
> count= 1 play= 301 ;
> count = 2 play = 302 ;
> play= 302 ; disable button
>
> ----------------------
>
> what I want is
> count= 0 play= 300 ;
> count= 1 play= 301 ;
> count = 2 play = 302 ;
> disable button
>
> can anyone tell me whether my logic is not right or the codes are not right?

Not from this, no--there's no way the output you've listed could have
been generated from this code -- one needs _PRECISELY_AND_IDENTICALLY_,
_WITHOUT_ANY_EDITING_ for presentation the code that generated the
output, also not edited...

For example, the output says "count" but the line w/o a semicolon is

 > handles.count= count

which would display "handles.count = 0" assuming count is 0 on entry.

Perhaps, in fact, therein is part of the problem--you increment
handles.count and then set it to count which will in essence undoes the
increment operation.

I suspect your problems are tied in with this problem...

--

Subject: repetitive counter (GUI)

From: ching l

Date: 15 Jul, 2009 07:34:02

Message: 12 of 14

dpb <none@non.net> wrote in message <h3j5kc$9je$1@news.eternal-september.org>...
> ching l wrote:
> ...
> > handles = guidata(gcbo);
> > if(handles.count<=2)
> > handles.count= handles.count+1;
> > handles.count = count


> > else
> > set(hObject, 'enable', 'off')
> > end
> > guidata(gcbo, handles);
> > ...
> > handles.randValue=299+handles.count;
> > play= handles.randvalue
> > ...
> >
> > --------------------
> > The results are
> >
> > count= 0 play= 300 ;
> > count= 1 play= 301 ;
> > count = 2 play = 302 ;
> > play= 302 ; disable button
> >
> > ----------------------
> >
> > what I want is
> > count= 0 play= 300 ;
> > count= 1 play= 301 ;
> > count = 2 play = 302 ;
> > disable button
> >
> > can anyone tell me whether my logic is not right or the codes are not right?
>
> Not from this, no--there's no way the output you've listed could have
> been generated from this code -- one needs _PRECISELY_AND_IDENTICALLY_,
> _WITHOUT_ANY_EDITING_ for presentation the code that generated the
> output, also not edited...
>
> For example, the output says "count" but the line w/o a semicolon is
>
> > handles.count= count
>
> which would display "handles.count = 0" assuming count is 0 on entry.
>
> Perhaps, in fact, therein is part of the problem--you increment
> handles.count and then set it to count which will in essence undoes the
> increment operation.
>
> I suspect your problems are tied in with this problem...
>
> --I'm sorry, i've accidentally edited that line when I try to make the codes look more reasonable..

it should be count= handles.count and that's the value that display on the matlab command window. I'm not sure what you mean by "you increment
handles.count and then set it to count which will in essence undoes the
increment operation." Would you mind to explain a little bit?

Anyway the codes should look like this-

handles = guidata(gcbo);

if(handles.count<=2)

handles.count= handles.count+1;

count = handles.count

else

set(hObject, 'enable', 'off')

end

guidata(gcbo, handles);

[audio, fs] = wavread('C:\a.wav');
samples{300} = {audio, fs};

[audio, fs] = wavread('C:\b.wav');
samples{301} = {audio, fs};

[audio, fs] = wavread('C:\b.wav');
samples{302} = {audio, fs};

handles.randValue=299+handles.count;

play= handles.randvalue

wavplay(samples{handles.randValue}{:}) ;
 
guidata(hObject, handles);

--------------------
The results are

count= 0 play= 300 ;
count= 1 play= 301 ;
count = 2 play = 302 ;
play= 302 ; disable button

----------------------

what I want is
count= 0 play= 300 ;
count= 1 play= 301 ;
count = 2 play = 302 ;
disable button

Subject: repetitive counter (GUI)

From: dpb

Date: 15 Jul, 2009 14:18:17

Message: 13 of 14

ching l wrote:
...
> ...I'm not sure what you mean by "you increment
> handles.count and then set it to count which will in essence undoes the
> increment operation." Would you mind to explain a little bit?

That's what the _POSTED_ code did which was NOT the actual code run.
It's absolutely impossible to debug when provided incorrect
information... :(

> Anyway the codes should look like this-

I've pared it down to the portion that has to do w/ the counter and
debugging i/o below...

Note the indenting to emphasize the logical structure; I strongly
recommend you adopt some sort of similar (but consistent) formatting;
you will find it will help immeasurably in reading your code.

> handles = guidata(gcbo);
> if(handles.count<=2)
> handles.count= handles.count+1;
> count = handles.count
> else
> set(hObject, 'enable', 'off')
> end
> guidata(gcbo, handles);
> ...
> handles.randValue=299+handles.count;
> play= handles.randvalue
...
> guidata(hObject, handles);
>
> --------------------
> The results are
>
> count= 0 play= 300 ;
> count= 1 play= 301 ;
> count = 2 play = 302 ;
> play= 302 ; disable button

OK, I see everything except what generated the "disable button" message?
  There's nothing in the code above I see that would have done it.

It's clear why everything else is as it is; the counter is
incremented/displayed for 0,1,2 as is "play". The line

play= handles.randvalue

is outside the if...else...end construct so it is executed every time
the function is called w/ the last value of the counter.

As noted, I can only assume you again added the "disable" button" to the
posting...

> ----------------------
>
> what I want is
> count= 0 play= 300 ;
> count= 1 play= 301 ;
> count = 2 play = 302 ;
> disable button

See above as to why the other "play =" message appeared.

You'll have to decide what the code is supposed to do once the counter
has been incremented.

If the real question is there's an additional event generated after you
think you have disabled the control, I don't know a thing about how ML
GUIs work at the control level; as noted I don't use anything except the
command window interface in ML.

If I were debugging I'd do something like

 > handles = guidata(gcbo);
 > if(handles.count<=2)
 > handles.count= handles.count+1;
 > count = handles.count
 > else
 > set(hObject, 'enable', 'off')
 > disp(['Disabled control'])
 > end
 > guidata(gcbo, handles);
 > ...
 > handles.randValue=299+handles.count;
 > play= handles.randvalue

to get the sequencing documented.

Also, when you post the results of any more testing, simply paste the
contents of the command window; don't rearrange lines, etc., other than
perhaps to squeeze out blank lines. It confuses the issue when the
code/output obviously don't match; sometimes it's just cosmetic, other
times as the previous post it changes the meaning entirely and makes the
whole post worse than meaningless. The point to be learned is you must
be precise to have any chance at all of getting meaningful response. A
secondary lesson is if you continue to create confusion by not adhering
to the above, folks will soon tire of going thru the exercise of
futility and just ignore the thread...

--

Subject: repetitive counter (GUI)

From: dpb

Date: 15 Jul, 2009 19:31:04

Message: 14 of 14

dpb wrote:
...
> If the real question is there's an additional event generated after you
> think you have disabled the control, ...

OK, it's obvious; I overlooked it myself though. :(

Walk thru the code line-by-line again for count == 1, 2, ... and trace
on a line of paper _when_ you actually disable the control.

All should become apparent at that point; there's nothing at all to do
w/ there being anything going on about the ML GUI controls at all (not
that I really thought so; only that, as I say, I was looking at the code
from the wrong standpoint before only concentrating on the "why" of the
outputs until I realized the underlying question)...

I'll leave it as an "exercise for the student" to come up w/ a fix at
least initially.... :)

--

Tags for this Thread

Everyone's Tags:

counter(2), gui

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
counter saqib saqib 30 Aug, 2009 06:24:01
counter ching l 13 Jul, 2009 16:39:08
gui ching l 13 Jul, 2009 16:39:08
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com