Thread Subject: making local variable global

Subject: making local variable global

From: Max

Date: 11 Mar, 2010 00:29:05

Message: 1 of 6

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

Subject: making local variable global

From: ImageAnalyst

Date: 11 Mar, 2010 02:30:26

Message: 2 of 6

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.

Subject: making local variable global

From: Max

Date: 11 Mar, 2010 22:52:04

Message: 3 of 6

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <521c53fa-84c0-446b-a7fc-f5b0a07f07a3@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.

Subject: making local variable global

From: Matt J

Date: 12 Mar, 2010 00:00:22

Message: 4 of 6

"Max " <nikitchmPublic@gmail.com> wrote in message <hnbs6k$acj$1@fred.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <521c53fa-84c0-446b-a7fc-f5b0a07f07a3@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

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

Subject: making local variable global

From: ImageAnalyst

Date: 12 Mar, 2010 02:02:34

Message: 5 of 6

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?

Subject: making local variable global

From: Loren Shure

Date: 12 Mar, 2010 13:44:56

Message: 6 of 6

In article <hnc06m$m41$1@fred.mathworks.com>,
mattjacREMOVE@THISieee.spam says...
> "Max " <nikitchmPublic@gmail.com> wrote in message <hnbs6k$acj$1@fred.mathworks.com>...
> > ImageAnalyst <imageanalyst@mailinator.com> wrote in message <521c53fa-84c0-446b-a7fc-f5b0a07f07a3@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

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

Contact us at files@mathworks.com