identity for a matrix cell

2 views (last 30 days)
Teresa
Teresa on 17 Jun 2015
Commented: Guillaume on 17 Jun 2015
Hello,
I would like to create an identity for a matrix cell
Something like:
x=A{2,1}
so that I can access A{2,1} with x
>> x=A{2,1}
x =
5
A{2,1} =
5
further on x is supposed to change continiously
x=x{2,1} is A{2,1}{2,1}
x=x{2,1} is A{2,1}{2,1}{2,1}
and so on
is that somehow possible

Accepted Answer

Guillaume
Guillaume on 17 Jun 2015
That type of variable is called a reference. It is not available in matlab, so no it's not possible.
You could emulate it after a fashion with a class but the syntax would be awkward. The interface would have to be something like:
x = cellreference('A', 2, 1); %create reference to cell {2, 1} of A
x.set(5); %set value of referenced cell to 5
x.get; %get value of referenced cell
Under the hood, it would have to use evalin or similar. It's not going to be fast.
  2 Comments
Teresa
Teresa on 17 Jun 2015
This does not seem to work. If I copy and paste your suggestion the following error message is returned
Undefined function 'cellreference' for input arguments of type 'char'.
I also can not find anything about cellreference in the help. Might that be the case because I am using 2012a and that version is too old?
Guillaume
Guillaume on 17 Jun 2015
cellreference would be a class that you would have to write yourselves, it's not something that comes with matlab.
I was just showing the syntax you would have to use with this hypothetical class. As the syntax is awkward and actually writing the set function is far from trivial (the get is easy), I don't see the benefit of it.
So for all intent and purpose, references in matlab are not possible.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!