Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: explicitly pass parameter by reference
Date: Mon, 6 Oct 2008 13:07:26 -0400
Organization: The MathWorks, Inc.
Lines: 37
Message-ID: <gcdgke$q2i$1@fred.mathworks.com>
References: <gcdds5$o8j$1@fred.mathworks.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1223312846 26706 144.212.105.187 (6 Oct 2008 17:07:26 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 6 Oct 2008 17:07:26 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
Xref: news.mathworks.com comp.soft-sys.matlab:493869



"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