user9085544
user9085544

Reputation: 13

Matlab uibutton callback

I'm working on a GUI in matlab and want to link a uibutton to a callback function. Right now, I'm just keeping it simple and have the following:

function []=set_time();

    function [] = set_time_callback(app)
        fprintf('\nhello world!')
    end

    app.UIFigure=uifigure;

    app.SetButton = uibutton(app.UIFigure,'push','Position',[23 29 106 22],'ButtonPushedFcn', @(app) set_time_callback(app));

end

It's yelling at me:

Error using set_time>@(app)set_time_callback(app) Too many input arguments.

Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 262) Error while evaluating Button PrivateButtonPushedFcn

Any tips?

Upvotes: 1

Views: 2664

Answers (2)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23685

function set_time()

    function set_time_callback(app)
        fprintf('\nhello world!')
    end

    app.UIFigure = uifigure();
    app.SetButton = uibutton(app.UIFigure,'push','Position',[23 29 106 22],'ButtonPushedFcn',@(obj,evt) set_time_callback(app));

end

The event handler (callback) you subscribe must respect a predefined signature, which must define all the input arguments (the first one is the sender object, the second one represents the event data).

For more information about callbacks, read this.

Upvotes: 0

Artyom Emelyanenko
Artyom Emelyanenko

Reputation: 1443

Callback functions require having at least 2 inputs - object and event. You get the error message because you only provided one input variable app and you are using it. When the callback function is called Matlab passes 2 additional input variables, so your function needs to have 3 input variable. I would try to change your function to something like that

function []=set_time();

    function [] = set_time_callback(obj, evt ,app)
        fprintf('\nhello world!')
    end

    app.UIFigure=uifigure;

    app.SetButton = uibutton(app.UIFigure,'push','Position',[23 29 106 22],'ButtonPushedFcn', @(obj, event) set_time_callback(obj, event, app));

end

Another way to pass an object to the function would be

app.SetButton = uibutton(app.UIFigure,'push','Position',[23 29 106 22],'ButtonPushedFcn', {@set_time_callback, app});

right now inside the function set_time_callback you are not using the variable app which you were trying to pass. You can get rid of this variable unless you are planning on adding some functionality. The other two variables are not used as well. You can use the ~ symbol instead of these variables in the function declaration, but you still need to keep space for 2 variables.

function []=set_time();

    function [] = set_time_callback(~, ~)
        fprintf('\nhello world!')
    end

    app.UIFigure=uifigure;

    app.SetButton = uibutton(app.UIFigure,'push','Position',[23 29 106 22],'ButtonPushedFcn', @set_time_callback);

end

using function handle you can get rid of these 2 variables in your callback function declaration

function []=set_time();

    function [] = set_time_callback()
       fprintf('\nhello world!')
    end

    app.UIFigure=uifigure;

    app.SetButton = uibutton(app.UIFigure,'push','Position',[23 29 106 22],'ButtonPushedFcn', @(obj, event) set_time_callback());

end

In this case, if you still want to pass a variable inside the function use this

function []=set_time();

    function [] = set_time_callback(varName)
       fprintf('\nhello world!')
    end

    app.UIFigure=uifigure;

    app.SetButton = uibutton(app.UIFigure,'push','Position',[23 29 106 22],'ButtonPushedFcn', @(obj, event) set_time_callback(app));

end

Upvotes: 1

Related Questions