|
"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
|