Got Questions? Get Answers.
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:
Any chance of passing data by reference in Matlab?

Subject: Any chance of passing data by reference in Matlab?

From: Michael

Date: 8 Dec, 2009 20:19:18

Message: 1 of 7

Hi,

I have yet another instance in my project where being able to pass data by reference to functions would be helpful to avoid copying large arrays back-and-forth thousands of times. Is there any chance this will be added in the future? Is there a technical reason why this is impossible? Using global vars seems a poor choice, the profiler always shows them as cpu hogs. Anyone else interested? Or am I missing something?

Cheers,
Michael

Subject: Any chance of passing data by reference in Matlab?

From: James Tursa

Date: 8 Dec, 2009 20:49:03

Message: 2 of 7

"Michael" <michael@circular-logic.com> wrote in message <hfmcc6$2h$1@fred.mathworks.com>...
> Hi,
>
> I have yet another instance in my project where being able to pass data by reference to functions would be helpful to avoid copying large arrays back-and-forth thousands of times. Is there any chance this will be added in the future? Is there a technical reason why this is impossible? Using global vars seems a poor choice, the profiler always shows them as cpu hogs. Anyone else interested? Or am I missing something?
>
> Cheers,
> Michael

You need to be more specific. When you call a function in MATLAB, the arguments are passed as shared data copies ... no data copying takes place. So other than the temporary structure for the argument it is equivalent to passing by reference as far as the data is concerned. If you modify the variable in the function, *then* a copy takes place if necessary. In previous versions of MATLAB this caused an extra copy to be created even if that extra copy was being assigned back into the original variable. Newer versions of MATLAB recognize when this situation is present, however, and don't make the extra copy. So there are not huge data copies constantly being made and created in the newer versions of MATLAB. I think you will have to show a small example of your code to demonstrate your problem.

James Tursa

Subject: Any chance of passing data by reference in Matlab?

From: Michael

Date: 9 Dec, 2009 01:24:05

Message: 3 of 7

> You need to be more specific. When you call a function in MATLAB, the arguments are passed as shared data copies ... no data copying takes place. So other than the temporary structure for the argument it is equivalent to passing by reference as far as the data is concerned. If you modify the variable in the function, *then* a copy takes place if necessary. In previous versions of MATLAB this caused an extra copy to be created even if that extra copy was being assigned back into the original variable. Newer versions of MATLAB recognize when this situation is present, however, and don't make the extra copy. So there are not huge data copies constantly being made and created in the newer versions of MATLAB. I think you will have to show a small example of your code to demonstrate your problem.
>
> James Tursa

Thanks James. Yes, I should be more specific!

I'm very happy to hear Matlab does at least some of the array passing by reference! Makes sense.

In what you say above, does "extra copy" mean that in older version of matlab *two* copies were made inside a function if a paramter was modified in the function?

In particular I'm interestd in passing back changes to parameter data. I can't pass them back via function return values because the function prototype has to match a template for ODE methods. So in this kind of situation it would be nice to truly pass by ref so 'data' would be modified in the calling scope as well:

data = 0;
function result = myFunc(data)
 data = data +1;
 result = 0;
end
%would like data to be 1 here

In another situation, if I pass in an array parameter, modify it and return the modified array like so

function result = myFunc(data)
 result = data +1;
end

arr = ones(1000);
arr = myFunc(arr);

Do you know if this would create any copies during the function call, or just operate by reference?

Thanks

Subject: Any chance of passing data by reference in Matlab?

From: Loren Shure

Date: 9 Dec, 2009 13:33:53

Message: 4 of 7

In article <hfmcc6$2h$1@fred.mathworks.com>, michael@circular-logic.com
says...
> Hi,
>
> I have yet another instance in my project where being able to pass data by reference to functions would be helpful to avoid copying large arrays back-and-forth thousands of times. Is there any chance this will be added in the future? Is there a technical reason why this is impossible? Using global vars seems a poor choice, the profiler always shows them as cpu hogs. Anyone else interested? Or am I missing something?
>
> Cheers,
> Michael
>

You may want to check out reference objects in the updated class system
(since R2008a).

--
Loren
http://blogs.mathworks.com/loren

Subject: Any chance of passing data by reference in Matlab?

From: Michael

Date: 9 Dec, 2009 20:19:05

Message: 5 of 7

> You may want to check out reference objects in the updated class system
> (since R2008a).
>

Great, thanks! I figure you mean using classes derived from "handle class"? Or is there a simpler, more direct way when I just want to get a handle to a regular matlab data object like an array? Couldn't find anything in help. That would be a nice feature, e.g. just doing:

data = ones(10000);
dataH = handle(data);
res = modifyData(dataH);

or better yet,

data = ones(10000,'handle');

Cheers

Subject: Any chance of passing data by reference in Matlab?

From: Loren Shure

Date: 9 Dec, 2009 20:45:45

Message: 6 of 7

In article <hfp0np$k0n$1@fred.mathworks.com>, michael@circular-logic.com
says...
> > You may want to check out reference objects in the updated class system
> > (since R2008a).
> >
>
> Great, thanks! I figure you mean using classes derived from "handle class"? Or is there a simpler, more direct way when I just want to get a handle to a regular matlab data object like an array? Couldn't find anything in help. That would be a nice feature, e.g. just doing:
>
> data = ones(10000);
> dataH = handle(data);
> res = modifyData(dataH);
>
> or better yet,
>
> data = ones(10000,'handle');
>
> Cheers
>

I meant deriving a new class from handle class.

--
Loren
http://blogs.mathworks.com/loren

Subject: Any chance of passing data by reference in Matlab?

From: Michael

Date: 9 Dec, 2009 21:12:20

Message: 7 of 7

Loren Shure <loren.shure@mathworks.com> wrote in message <MPG.2589d6855d1ad424989a86@news.mathworks.com>...
> In article <hfp0np$k0n$1@fred.mathworks.com>, michael@circular-logic.com
> says...
> > > You may want to check out reference objects in the updated class system
> > > (since R2008a).
> > >
> >
> > Great, thanks! I figure you mean using classes derived from "handle class"? Or is there a simpler, more direct way when I just want to get a handle to a regular matlab data object like an array? Couldn't find anything in help. That would be a nice feature, e.g. just doing:
> >
> > data = ones(10000);
> > dataH = handle(data);
> > res = modifyData(dataH);
> >
> > or better yet,
> >
> > data = ones(10000,'handle');
> >
> > Cheers
> >
>
> I meant deriving a new class from handle class.

OK thanks.

Tags for 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