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?

5
  • 2
    Straightforward way: just copy it into another variable and destroy the old one. Also, this is a possible duplicate of this question. Commented Jun 15, 2012 at 6:04
  • 2
    "...the data is very large, meaning I can not copy it to another variable and delete the first one..." Commented Jun 15, 2012 at 8:21
  • Chris: Does not matter. MATLAB uses references, A=B does not mean B is copied. Commented Jun 15, 2012 at 9: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. Commented Jun 6, 2014 at 7: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. Commented Jun 9, 2014 at 15:50

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.