Thread Subject: Q: How to pass by reference even when changing a value in a function?

Subject: Q: How to pass by reference even when changing a value in a function?

From: anderson@satchmo.cs.colostate.edu (Chuck Anderson)

Date: 28 May, 1997 17:25:37

Message: 1 of 7

I know that matlab effectively passes by value when the value of an argument
is changed inside a function. Is there any way to maintain it as a call by
reference? I want to change a component of a structure passed in to a
function without having to generate a new copy of the structure to be returned
by the function.

----------------------------------------------------------------------
Chuck Anderson
Department of Computer Science anderson@cs.colostate.edu
Colorado State University http://www.cs.colostate.edu/~anderson
Fort Collins, CO 80523-1873 office: 970-491-7491, FAX: 970-491-2466

Subject: Q: How to pass by reference even when changing a value in a function?

From: Steve Eddins

Date: 29 May, 1997 06:45:55

Message: 2 of 7


Chuck Anderson (anderson@satchmo.cs.colostate.edu) wrote:

> I know that matlab effectively passes by value when the value of an
> argument is changed inside a function. Is there any way to maintain
> it as a call by reference? I want to change a component of a
> structure passed in to a function without having to generate a new
> copy of the structure to be returned by the function.

There is no way to do what you ask, although it is recorded in our
enhancements database for future consideration.

However, because of memory management improvements in MATLAB 5, you
might not really need to do this. (Since you mention structures it's
a good bet that you're using MATLAB 5.) MATLAB doesn't make copies of
array data until necessary. This is easiest to explain by example:

A.left = magic(50);
A.right = rand(50);

B = A; % new variable, but still only
                     % one copy of magic(50), rand(50), shared by A
                     % and B.

B.left(1,1) = 5; % MATLAB recognizes that it now needs to copy the
                     % data from A.left into B.left before it performs
                     % the assignment. There is still only one copy
                     % of rand(50), which is being shared by A.right
                     % and B.right.

So if you pass a structure to a function, make a copy of it inside the
function, change one field of it, and return the result, you don't get
unnecessary data copies. (There is some small overhead associated
with each "owner" of shared data, 100 bytes or so.)

Does that help? Do you still want pass-by-reference?

Regards,

Steve

--
Steve Eddins eddins@mathworks.com
The MathWorks, Inc. http://www.mathworks.com

Subject: Q: How to pass by reference even when changing a value in a function?

From: Arnaud Delorme

Date: 31 Mar, 2009 16:48:01

Message: 3 of 7

Yes, there is a way and it took me forever to find it. It is not elegant but it works. It consists in declaring a global variable in the scope of the caller and the called function, then erase all other variable, modify the global variable, and copy it back to the variable in the called function.

Tricky,

A. Delorme

function varout = testpassbyref(values);

global test;
evalin('caller', [ 'clear ' inputname(1) ';']);
evalin('caller', 'global test;');
test = values;
clear values;

test(1) = 3;

evalin('caller', [ inputname(1) ' = test;' ]);
evalin('caller', 'clear global test;');
clear global test;

Subject: Q: How to pass by reference even when changing a value in a function?

From: James Tursa

Date: 1 Apr, 2009 07:20:03

Message: 4 of 7

anderson@satchmo.cs.colostate.edu (Chuck Anderson) wrote in message <5mieth$ota@satchmo.cs.colostate.edu>...
> I know that matlab effectively passes by value when the value of an argument
> is changed inside a function. Is there any way to maintain it as a call by
> reference? I want to change a component of a structure passed in to a
> function without having to generate a new copy of the structure to be returned
> by the function.
>
> ----------------------------------------------------------------------
> Chuck Anderson
> Department of Computer Science anderson@cs.colostate.edu
> Colorado State University http://www.cs.colostate.edu/~anderson
> Fort Collins, CO 80523-1873 office: 970-491-7491, FAX: 970-491-2466

Yes, you can do it in a mex routine. But you might not be saving much ... re Steven Lord's comments. The mex routine would probably only save you some of the overhead he mentions, and may not be worth the effort.

James Tursa

Subject: Q: How to pass by reference even when changing a value in a function?

From: Tilman Sumpf

Date: 30 Sep, 2010 09:52:04

Message: 5 of 7

ok, this is a pretty old post, but since I had a similar problem i'd like to mention the workaround to create a parameter as an instance of a handle class...:

% define a class which holds the parameter myValue
classdef myClass < handle
    properties
        myValue = [];
    end
    methods
        function obj = myClass() % constructor
        end
    end
end


function main()

   obj = myClass; % construct an object of myClass
   obj.myValue = 5; % set some fancy values to the object

   myFunction(obj); % pass the object to a nice function

   disp(obj.myValue) % verify that myValue has changed

end

function myFunction(obj)
    obj.myValue = obj.myValue + 1;
end

Subject: Q: How to pass by reference even when changing a value in a function?

From: Joel F

Date: 6 Oct, 2010 19:58:19

Message: 6 of 7

Thank you much, this works very well! I experienced a 100% speed up in my code by using this pass by reference work around.

"Tilman Sumpf" <tsumpf@gwdg.de> wrote in message <i81mk4$2ft$1@fred.mathworks.com>...
> ok, this is a pretty old post, but since I had a similar problem i'd like to mention the workaround to create a parameter as an instance of a handle class...:
>
> % define a class which holds the parameter myValue
> classdef myClass < handle
> properties
> myValue = [];
> end
> methods
> function obj = myClass() % constructor
> end
> end
> end
>
>
> function main()
>
> obj = myClass; % construct an object of myClass
> obj.myValue = 5; % set some fancy values to the object
>
> myFunction(obj); % pass the object to a nice function
>
> disp(obj.myValue) % verify that myValue has changed
>
> end
>
> function myFunction(obj)
> obj.myValue = obj.myValue + 1;
> end

Subject: Q: How to pass by reference even when changing a value in a function?

From: Matt J

Date: 6 Oct, 2010 20:23:20

Message: 7 of 7

"Joel F" <jcforman@umich.edu> wrote in message <i8ikcr$3d0$1@fred.mathworks.com>...
> Thank you much, this works very well! I experienced a 100% speed up in my code by using this pass by reference work around.
=====

That's great, but be aware of certain problems with using handle classes:


http://www.mathworks.com/matlabcentral/newsreader/view_thread/288746#769470

Tags for this Thread

Everyone's Tags:

oop

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
oop per isakson 2 Oct, 2010 12:40:44
rssFeed for this Thread

Contact us at files@mathworks.com