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

New to MATLAB?

Pass variable by reference to function

Asked by Adam

Adam (view profile)

on 28 Sep 2012
Latest activity Commented on by per isakson

per isakson (view profile)

on 22 Feb 2015
Accepted Answer by Daniel

Daniel (view profile)

I have read that the only way to pass by reference with a function is if you pass a handle. Is there a matlab call to get the "handle" of a variable or structure?

0 Comments

Adam

Adam (view profile)

Products

5 Answers

Answer by Daniel

Daniel (view profile)

on 1 Oct 2012
Accepted answer

While I think IA's answer is the better way to do it. If you really want to do what you are talking about you can create a subclass of handle.

classdef myClass < handle
	properties
		data
	end
	methods
		function h = myClass(data)
		  h.data = data	;
		end
	end
end

Then defining a function (which could be a method) like

function myFunction(h)
	h.data = h.data+2;
end

lets you do something like

h = myClass(0);
myFunction(h);
h.data
ans =
   2

of course things like h+2 will not work unless you implement a plus method.

1 Comment

Alessandro

Alessandro (view profile)

on 27 Feb 2013

Nice post ! Matlab should have something like this as a standard object ! The problem with this class is that it still copy the value from workspace to the class but is a good workaround.

Daniel

Daniel (view profile)

Answer by Image Analyst

Image Analyst (view profile)

on 29 Sep 2012

For all practical purposes, you can get the same effect by passing the input argument back out. For example, in the function definition:

function myVariable = ChangeMyVariable(myVariable)
  myVariable = 2 * myVariable;

Then to call it, do this:

myVariable = ChangeMyVariable(myVariable);

And, obviously, myVariable could be anything, such as a structure like you mentioned. For all intents and purposes, this is the same as the code you'd use if it were pass by reference:

ChangeMyVariable(myVariable);

The net effect is you get your variable changed, it's just slightly different syntax.

18 Comments

Titus Edelhofer

Titus Edelhofer (view profile)

on 8 Jan 2015

Hi Lucas,

the programming pattern that ImageAnalyst showed, namely

function Y = someFun(x, Y, z)
Y = x + Y;

involves no copying of Y at all, provided, that you

  • call it like var = someFun(x, Var, y), i.e. both the function declaration and the calling function use the same variable names for input and output
  • Your MATLAB version is not too old: this optimization was introduced somewhere around 2010.

Give it a try with a large variable. You will see in the task manager no copy is done.

Titus

Royi Avital

Royi Avital (view profile)

on 22 Feb 2015

Hi, I tried your trick with very large matrix. By the jump in the memory consumption I can assure you it is not working by reference.

per isakson

per isakson (view profile)

on 22 Feb 2015

I think there is a typo in the comment by Titus Edelhofer, it should read

    var = someFun(x, var, y),

rather than

    var = someFun(x, Var, y),

@Royi, Which release are you running?

Image Analyst

Image Analyst (view profile)

Answer by per isakson

per isakson (view profile)

on 28 Sep 2012
Edited by per isakson

per isakson (view profile)

on 28 Sep 2012

No, there is no way to pass a variable by reference. See Memory Management for Functions and Variables.

Handle objects, http://www.mathworks.se/help/matlab/matlab_oop/user-defined-classes.html, are passed by "reference".

2 Comments

Adam

Adam (view profile)

on 28 Sep 2012

I understand that only handles can be passed by reference. Is there a way of obtaining a handle for a variable?

per isakson

per isakson (view profile)

on 28 Sep 2012

No, not with documented Matlab. And not with http://undocumentedmatlab.com/ as far as I know.

Why do you want to pass by reference?

per isakson

per isakson (view profile)

Answer by James Tursa

James Tursa (view profile)

on 29 Sep 2012

I will ask again what others have already asked. Why do you want the "handle" to the variable? What advice we give you will depend on the answer to that question. FYI, by default MATLAB passes a shared data copy of the arguments to the function ... i.e., there is no data copy involved. So it is already efficient in that sense. But we really need to know what you are doing with the variable inside the function in order to best answer your question, and whether passing by "reference" makes sense for your application.

2 Comments

Adam

Adam (view profile)

on 1 Oct 2012

This isn't a memory question as much a code cleanliness, reuse, and good practices question as well as my own edification. I'm not looking for coding advice, but an answer to the question of if it is fundamentally possible to get the "handle" of a variable or structure?

Walter Roberson

Walter Roberson (view profile)

on 3 Oct 2012

If it is a question about code cleanliness, reuse, and good practices, then you are looking for coding advice, and IA's recommendation to use a return value is not "imposed" is an appropriate "good practices" response. Hidden modification of variables is not good practice; it is, for example, emphatically disallowed in Yourdon & Constantine's "Structured Design" (1979)

James Tursa

James Tursa (view profile)

Answer by Alessandro

Alessandro (view profile)

on 16 Apr 2013

Hello I would say a possible solution is the following (without using classes):

%Reference to some matlab Data
  function reference = globaldata()
      data = [];
        reference = struct('GET',@GET,'SET',@SET);
        function dataout = GET()
            dataout = data;
        end
        function SET(datain)
            data =datain;
        end
    end

I saw something like this for making linked lists in matlab on stack overflow.

0 Comments

Alessandro

Alessandro (view profile)

Contact us