15

I want to change a variable name before I export it to the global enviroment, the data is very large, meaning I can not copy it to another variable and delete the first one.

the data is loaded to certain variables and that I cannot change as well, it is used multiple times in differnet background jobs, so what I want to do is rename it and send it renamed so the jobs won't mix and after for the next job load and rename again etc.

basically is to do in the command window what I can do with the mouse in the workspace....

does anyone knows how to do it?

jarhead
  • 1,821
  • 4
  • 26
  • 46
  • 2
    Straightforward way: just copy it into another variable and destroy the old one. Also, this is a possible duplicate of [this question](http://stackoverflow.com/questions/5964639/matlab-renaming-workspace-elements-from-command-window). – Eitan T Jun 15 '12 at 06:04
  • 2
    "...the data is very large, meaning I can not copy it to another variable and delete the first one..." – Chris Taylor Jun 15 '12 at 08:21
  • Chris: Does not matter. MATLAB uses references, A=B does not mean B is copied. – Hannes Ovrén Jun 15 '12 at 09:19
  • @HannesOvrén when you say " MATLAB uses references, A=B does not mean B is copied." then it means after copying, if B is changed only then A is actually created and takes up required memory space. – Nishant Jun 06 '14 at 07:47
  • Sure. But the point is that you do `clear A` so that you don't mistakenly do that (like the accepted answer and above comment tells you). I was just pointing out that assigning a name to a new variable does not copy the data as it is simply a reference to the same data. – Hannes Ovrén Jun 09 '14 at 15:50

2 Answers2

16

When assigning variable names, matlab uses a "lazy copy", so there is no reason why:

new_name = old_name;
clear old_name;

shouldn't work.

stanri
  • 2,922
  • 3
  • 25
  • 43
2

The only way that I can think of to do this without a memory copy would be to wrap the original data in an object that is a subclass of the handle class.

http://www.mathworks.co.uk/help/techdoc/matlab_oop/brfylzt-1.html

You can then 'copy' the handle class using normal syntax

 classB=classA

..but you are only making an alias for the same data (changes to classB are reflected in classA). This is the closest thing in matlab to pointer-like semantics.

For example, create a file called myHandle and paste the following text to define the class . .

classdef myHandle < handle

    properties
        data
        moreData
    end

    methods
    end

end

You can then use this just like a regular structure. From the command line do ..

>> x = myHandle

x = 

  myHandle handle

  Properties:
        data: []
    moreData: []

  Methods, Events, Superclasses

... we can then populate the data . . .

>> x.data = [1 2 3 4];
>> x.moreData = 'efg';

... once the original object has been populated with data, an alias of the original data can be made by typing . .

>> y = x

The interesting thing happens to x when y is modified, i.e.

>> y.data = [33 44 55 66];
>> disp(x)
x = 

  myHandle handle

  Properties:
        data: [33 44 55 66]
    moreData: 'f'

  Methods, Events, Superclasses

You can even clear one of the alias names . .

 clear x

..and the data will still be available in the other handle for the data, y. The memory is only freed when there are no more handles referring to it (when the reference count has reached zero).

learnvst
  • 15,455
  • 16
  • 74
  • 121