0

I have two functions:

function [] = func_one()
     S.pb = uicontrol('style','push','unit','pix','posit',[20 20 260 30],
                      'string','Print Choices','callback',{@func_two,S});

and I have the second function:

   function [a] = func_two(varargin)
       a = 'alon';
   end

I want func_one to return the variable a of func_two. How can I do that please?

I tried:

 function [a] = func_one()

But I guess I have to do something with 'callback',{@func_two,S})

Thank you all!

1
  • @Memming, I changed my question. if you can, please look at this Commented May 24, 2012 at 21:08

2 Answers 2

3

If, as you say, you want func_one to return the value a in func_two then the easiest way to do this without using a callback is:

function [a] = func_one()
     S.pb = uicontrol('style','push','unit','pix','posit',[20 20 260 30],
                      'string','Print Choices');

     a = func_two()

The above will allow you to say run a=func_one and a will be the string 'alon'.

If you really really want func_two() to be a callback of your pushbutton, and you want a='alon' to be assigned in the workspace of func_one (the function that calls func_two) then put this in func_two

assignin('caller','a',a)

And if neither is what you want, then maybe you can indicate why you want func_one to return what func_two returns - like the exact interaction you are hoping to have with your GUI and how it differs from what you're actually experiencing.

Sign up to request clarification or add additional context in comments.

Comments

2

If you are designing a GUI programmatically, I suggest you use nested functions to share data. Example:

function IncrementExample()
    x = 0;
    uicontrol('Style','pushbutton', 'String','(0)', ...
        'Callback',@callback);

    function callback(o,e)
        %# you can access the variable x in here
        x = x + 1;

        %# update button text
        set(o, 'String',sprintf('(%d)',x))
        drawnow
    end
end

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.