bugGNU Octave - Bugs: bug #60153, uicontrol pushbutton has different...


bug #60153: uicontrol pushbutton has different behavior for 'Value' field from Matlab

Submitted by:  None
Submitted on:  Mon 01 Mar 2021 03:07:54 PM UTC  
Category:  GUI Severity:  3 - Normal
Priority:  1 - Later Item Group:  Matlab Compatibility
Status:  Confirmed Assigned to:  None
Originator Name:  Heinz-Georg Fehn Originator Email:  -email is unavailable-
Open/Closed:  Open Release:  dev
Operating System:  Any

Add a New Comment (Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


( Jump to the original submission)

Wed 03 Mar 2021 06:07:33 PM UTC, comment #6: 

@Markus: This is good news.  This means Octave is executing the same ordering of updating value and then calling the callback as Matlab.  It is only the Pushbutton style that differs.  And why Matlab chose to be inconsistent with themselves, for this particular uicontrol style, is anyone's guess.  I'm not sure we even want to mimic Matlab here, and there are other ways (such as gcbo()) to accomplish the same goal.

Rik <rik5>
Project Administrator
Wed 03 Mar 2021 05:34:19 PM UTC, comment #5: 

In Matlab R2020b, if the checkbox was previously un-checked, it is checked as soon as I release the mouse button. With your callback function "ans=1" appears in the command window.
When I click the checkbox again, it unchecks as soon as I release the mouse button and "ans=0" appears in the command window.

So, it looks like the callback executes just after the update of the value property.
That is different from the pushbutton behavior.

Markus Mützel <mmuetzel>
Project Member
Wed 03 Mar 2021 05:18:26 PM UTC, comment #4: 

Okay, so this looks less dire than I thought.  The important thing is that both Octave and Matlab run callbacks on the ButtonRelease event.  The exact ordering of when object properties are updated, and when graphics callbacks are executed, seems implementation dependent.

For most of the uicontrols this won't matter.  For example, a slider that you move to a new position.  The "Value" property is unchanged by a ButtonRelease event so it doesn't matter when the "Callback" function is exactly executed.  However, for the various button types it might.

What does Matlab do with this code?

function mycb (h, e)
  get (h, 'value')

close all
h = uicontrol ('style', 'checkbox', 'string', 'Box1', 'callback', @mycb);

Now, click and hold the mousebutton on the checkbox.  When you release the mouse, is the "Value" field that is printed always the previous value of the checkbox or the value that the checkbox has just changed to?

For example, if the checkbox is blank and you click on it the Value field printed will either be '0' (last value) or '1' (the checked state that it is changing to).

For Octave, the answer is definite.  Octave updates values, and then runs callbacks, so the answer printed will be '1'.

Rik <rik5>
Project Administrator
Wed 03 Mar 2021 10:41:58 AM UTC, comment #3: 

I am not sure if this is just an "implementation accident" or intended behavior in Matlab.
If you are only interested in which button was clicked in the callback, you could also use `gcbo`. That should be working both in Matlab and Octave.
In Rik's example, `cb_uipushbutton` could look like this:

function cb_uipushbutton ()
global h_b;

gcbo == h_b

Lowering priority because the same can be achieved using documented behavior.

Markus Mützel <mmuetzel>
Project Member
Wed 03 Mar 2021 10:30:07 AM UTC, comment #2: 

I ran the test case from comment #1 in Matlab R2020b.
The callback seems to execute on the release of the mouse button.

The button gets a blue-ish hue when it is selected. Even after releasing the mouse button, the pushbutton keeps that color until I click somewhere else (in the same figure or a different program).
However, I see the following in the command line while the button is still blue-ish:

>> global h_b
>> get(h_b, 'Value')

ans =

  4×1 cell array


So the color change doesn't relate to the "Value" property.

It looks like Matlab executes the callback on the ButtonRelease event but (just) before the "Value" property is updated.

Markus Mützel <mmuetzel>
Project Member
Mon 01 Mar 2021 06:21:21 PM UTC, comment #1: 

I've simplified the code a bit and uploaded it for testing.  The new files are tst_uipushbutton.m (run this one) and cb_uipushbutton.m (graphics callback function).

Try left-clicking on one of the 4 uipushbuttons and holding the mouse down.  For me, the callback function does not run.  Then release the mouse button, and now the callback function runs.

If you see the same behavior then I think what is happening is that Matlab is running the specified callback on a ButtonDown event while Octave is running the specified callback on a a ButtonRelease event.  Because Octave is running after the release of the Button there is nothing selected and the Value property is always 0.  Could you test in Matlab to see if the callback is executed immediately on a ButtonDown event?

(file #50960, file #50961)

Rik <rik5>
Project Administrator
Mon 01 Mar 2021 03:07:54 PM UTC, original submission:  

Using   h_b(ii)=uicontrol(... in a loop

and get(h_b,'value') in the callback function will not given a result which button was hit.

In Matlab the hselected cellarray of h_b(ii) will be marked with 1, the others are zero.

See added code.


function ui_test

global h_b

close all
scrsz = get(0,'ScreenSize');
if strcmpi(b,'octave')
    figure(1,'Units','pixels','Position',[20 src_p wide blatt],'MenuBar','none','Color',[0.8 0.8 0.8],...
    set(1,'Units','pixels','Position',[20 src_p wide blatt],'MenuBar','none','Color',[0.8 0.8 0.8],...

for ii=1:4
    h_b(ii)=uicontrol('Style','pushbutton','Position',[10 pos-ii*30 30 30],'string'...

Callback function for test

function test()
global h_b


will show:

>> ans =

  [1,1] = 0
  [2,1] = 0
  [3,1] = 0
  [4,1] = 0

whereas the same call in Matlab will show

ans =

  4×1 cell array


if 3rd button was selected
Best regards

Heinz-Georg Fehn



(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

Attached Files
file #50960:  tst_uipushbutton.m added by rik5 (628B - text/x-matlab)
file #50961:  cb_uipushbutton.m added by rik5 (65B - text/x-matlab)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by rik5 (Updated the item)
  • -email is unavailable- added by None (Submitted the item)

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.




    Follow 9 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-03-03 rik5 Priority3 - Low => 1 - Later
    2021-03-03 mmuetzel Priority5 - Normal => 3 - Low
    2021-03-03 mmuetzel CategoryOctave Function => GUI
        Release6.2.0 => dev
    2021-03-01 rik5 SummaryProduces no resulat when using an array => uicontrol pushbutton has different behavior for 'Value' field from Matlab
    2021-03-01 rik5 Attached File- => Added tst_uipushbutton.m, #50960
        Attached File- => Added cb_uipushbutton.m, #50961
        StatusNone => Confirmed
        Operating SystemMicrosoft Windows => Any

    Back to the top

    Powered by Savane 3.6