Don't you know, is there a way to make the local variable global without Matlab warning that this ability will be discontinued in the future (of course, the warning can be suppressed. I simply want this method to keep working the future versions)?
I'm working with data, which can be huge sometimes (Gbs) and I'd like to be able to make it global, and treat differently, when the size of the variables exceeds a particular limit.
Thank you,
Max
I know I can. What I'm worried about is that the message says this ability to turn local variable into global will be discontinued in the future. So I'm looking for an alternative way doing it.
Are you sure you need to do this? No matter how huge your data is, as long as you never make any changes to it, you can pass it around freely to functions and MATLAB will not make any deep copies of it. Make sure you are familiar with the material here
http://blogs.mathworks.com/loren/2006/05/10/memory-management-for-functions-and-variables/
If you are making changes to your data, consider wrapping it inside a handle object. For example, using the classdef at the bottom of this post, I can do as follows
obj=refwrap;
obj.data=MyHugeData;
clear MyHugeData
I can now pass obj around to any function I like, and access and change MyHugeData through obj.data pretty much any way I like, and no deep copies of the data will ever be made.
classdef refwrap < handle
%REFWRAP - a generic handle class for arbitrary MATLAB data
%
% obj=refwrap(X)
%
%where dataInput is any MATLAB variable will result in a handle object
%obj with obj.data=X
%
%This is useful, for example, if we want to force X to be processed by
%reference in a function call, i.e.,
%
% obj=refwrap(X); clear X
% func(obj,...)
%
%would allow func() to process obj.data arbitrarily, without making a 2nd deep
%copy of X, and so that obj.data in the base workspace would feel the
%changes made by func.
properties
data;
end
methods
function obj=refwrap(dataInput)
if nargin==0, return; end
obj.data=dataInput;
end
end
end
Another option is to have the data in the parent function of a nested
function. Then return the handle to the nested function and use that to
update the data - only 1 instance - no extraneous copies.
--
Loren
http://blogs.mathworks.com/loren
http://matlabwiki.mathworks.com/MATLAB_FAQ