Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

making local variable global

1 view
Skip to first unread message

Max

unread,
Mar 10, 2010, 7:29:05 PM3/10/10
to
Hi,

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

ImageAnalyst

unread,
Mar 10, 2010, 9:30:26 PM3/10/10
to
Max
I never see a warning in my command window when my code hits a
"global" statement. If yours does, you can turn off warnings with the
warning() function.

Max

unread,
Mar 11, 2010, 5:52:04 PM3/11/10
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <521c53fa-84c0-446b...@y11g2000yqh.googlegroups.com>...

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.

Matt J

unread,
Mar 11, 2010, 7:00:22 PM3/11/10
to
"Max " <nikitch...@gmail.com> wrote in message <hnbs6k$acj$1...@fred.mathworks.com>...
==============

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

ImageAnalyst

unread,
Mar 11, 2010, 9:02:34 PM3/11/10
to
Max:
I ran some code, and I also looked in the help. Nowhere did I see
this message you're referring to. Where did you see it, or how did
you generate it?

Loren Shure

unread,
Mar 12, 2010, 8:44:56 AM3/12/10
to
In article <hnc06m$m41$1...@fred.mathworks.com>,
mattja...@THISieee.spam says...

> "Max " <nikitch...@gmail.com> wrote in message <hnbs6k$acj$1...@fred.mathworks.com>...
> > ImageAnalyst <imagea...@mailinator.com> wrote in message <521c53fa-84c0-446b...@y11g2000yqh.googlegroups.com>...
> > > Max
> > > I never see a warning in my command window when my code hits a
> > > "global" statement. If yours does, you can turn off warnings with the
> > > warning() function.
> >
> > 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
>
<example deleted>

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

0 new messages