Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Callbaks, Passing by reference, setting output

Subject: Callbaks, Passing by reference, setting output

From: Karl Trumstedt

Date: 14 Sep, 2007 10:01:49

Message: 1 of 6

Hi,

I have an textcontrol defining a callback {@function, var1,
var2}

I want to assign var1 the value of the textbox + the value
of var2. This is no problem, but since matlab passes by
value (?) the values does not change var1. How can I either
pass by reference or define an output with my callback?

a callback like this:
var1 = function(var1, var2)

/Karl
Developer @ Lundin Mining AB
http://www.lundinmining.com/index.php
Developer @ Auktionshuset Kolonn AB http://www.kolonn.se

Subject: Callbaks, Passing by reference, setting output

From: Karl Trumstedt

Date: 14 Sep, 2007 10:11:07

Message: 2 of 6

I made a temporary solution by passing the name of the
variable to the function and using eval to assign the value.
Since I am using nested function this works, but I am still
interested in other solutions.

Cheers
/Karl
Developer @ Lundin Mining AB
http://www.lundinmining.com/index.php
Developer @ Auktionshuset Kolonn AB http://www.kolonn.se

Subject: Callbaks, Passing by reference, setting output

From: John

Date: 14 Sep, 2007 13:48:18

Message: 3 of 6

You have mentioned the solution yourself -
var1 = function(var1, var2)

Why did you not use this to return the output from your
callback ?

If you want to know more about why matlab uses call by
value, read this -
http://www.mathworks.com/support/solutions/data/1-15SO4.html?solution=1-15SO4




"Karl Trumstedt" <karltru@gmail.com> wrote in message
<fcdm6c$vk$1@fred.mathworks.com>...
> Hi,
>
> I have an textcontrol defining a callback {@function, var1,
> var2}
>
> I want to assign var1 the value of the textbox + the value
> of var2. This is no problem, but since matlab passes by
> value (?) the values does not change var1. How can I either
> pass by reference or define an output with my callback?
>
> a callback like this:
> var1 = function(var1, var2)
>
> /Karl
> Developer @ Lundin Mining AB
> http://www.lundinmining.com/index.php
> Developer @ Auktionshuset Kolonn AB http://www.kolonn.se

Subject: Callbaks, Passing by reference, setting output

From: Malcolm Lidierth

Date: 14 Sep, 2007 14:05:07

Message: 4 of 6

Pass the handle of the text box and have the callback read
the text e.g.

var1=text(0.1,0.1,'MyText');
var2=pi;
set(obj,'Callback', {@myCallback, var1, var2})



function(hObj, EventData, var1, var2)
str=get(var1,'String');
.
.
.
end

Subject: Callbaks, Passing by reference, setting output

From: Karl Trumstedt

Date: 14 Sep, 2007 14:29:33

Message: 5 of 6

I have defined the callback like this

'Callback', {@functionname, var1, var2}


function functionname(hObject, eventdata, var1, var2)
value = str2double(get(hObject, 'String'))
var1 = value + var2;
end


So this function is supposed to assign the value of the
caller and var2 to the variable var1 which exists in the
main function.

'Callback', var1 = {@functionname, var1, var2}
does not seem to work.



/Karl
Developer @ Lundin Mining AB
http://www.lundinmining.com/index.php
Developer @ Auktionshuset Kolonn AB http://www.kolonn.se

Subject: Callbaks, Passing by reference, setting output

From: Malcolm Lidierth

Date: 14 Sep, 2007 16:23:53

Message: 6 of 6

I see the problem.

By their nature, callbacks can not have left-hand side
arguments.

You need to pass your answer back another way e.g. by
putting it in the user data area of the text object from
the callback:

set(hObject,'UserData',value+var2)

then use the appropriate get method in your main code.

Then of course you need to get your code to hang up until
the callback has done it job (see uiwait and uiresume).

Tags for this Thread

No tags are associated with this thread.

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.

Contact us