Thread Subject: explicitly pass parameter by reference

Subject: explicitly pass parameter by reference

From: Young Zhou

Date: 6 Oct, 2008 16:20:21

Message: 1 of 2

I want to know how to pass parameters by reference? I need to pass and big chunk of data to a function, and this function does not change data.

I found an article (http://www.mathworks.com/support/solutions/data/1-15SO4.html?solution=1-15SO4) which states that Matlab will determine whether to pass by value or pass by reference automatically. I just want to confirm it.

Subject: explicitly pass parameter by reference

From: Steven Lord

Date: 6 Oct, 2008 17:07:26

Message: 2 of 2


"Young Zhou" <predicate_zy@yahoo.com> wrote in message
news:gcdds5$o8j$1@fred.mathworks.com...
>I want to know how to pass parameters by reference? I need to pass and big
>chunk of data to a function, and this function does not change data.

You cannot, unless your parameter is an instance of a handle class.

> I found an article
> (http://www.mathworks.com/support/solutions/data/1-15SO4.html?solution=1-15SO4)
> which states that Matlab will determine whether to pass by value or pass
> by reference automatically. I just want to confirm it.

MATLAB uses copy-on-write, which means that if you don't change the data
inside your function, it will not be copied into the workspace of the
function. For instance, in:

function y = myfunction(x)
y = x(1);

you're not writing to x, so even if x is very large it won't be copied into
the workspace. You can see this:

% open whatever memory profiling tool you want
% on Windows, you can use Task Manager.
% on other platforms, you can use "top"
% When you execute this command, the memory usage of MATLAB should increase
significantly
x = ones(10000);
% When you execute this command, it shouldn't.
y = myfunction(x);

--
Steve Lord
slord@mathworks.com

Tags for this Thread

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com