This question already has answers here:
Copy MATLAB object of a handle class
(1 answer)
way to copy a class that is a Handle Class?
(1 answer)
Closed 1 year ago.
I have an object, which is a matlab class. I want to copy a few members of the class to other variables, but matlab is copying the reference and not the value.
The class definition is classdef MyObj < handle, and I have a member A where A is iteself a struct. So, A looks like this:
A.num1
A.num2
A.num3
So, I create A: A = MyObj, then assign some values: A.num1=1, A.num2=2, and A.num3=3. Now, I want to copy members of A to another variable. For example, c(1)=A.num1, then I want to modify A, and copy the element to another variable c(2)=A.num1, etc... However, the copy is a reference, so I end up with all variable c(1), c(2), etc... all equal to the same value.
This is an example to illustrate my question. The object I am working with has much more complex structs. How can I make a deep copy?
Related
I tried to change an attribute value of a class by invoking one of its member function:
p1 = tank();
p1.checkOri(p1);
And in the class definition, I have that static method:
classdef tank
properties
value
...
end
methods
...
methods (Static)
function obj = checkOri(obj)
if (CONDITION) %the thing I want it to do
obj.value = EXPRESSION;
...
Yet this checkOri method is not working. But if I write this method in the main file, or to say altering the value of p1-the instance of class tank-it works perfectly:
p1 = tank();
p1.checkOri(p1);
if (CONDITION) %the thing I want it to do
p1.value = EXPRESSION;
It works perfectly.
I wonder what caused this. From my experience with other programming languages, invoking method should have worked, is it because of some tricks with Matlab syntax or static method? How could I fix it so that this method would work?
So, as #Navan in the comment said, handle class could be a solution.
It appears Matlab has a similar parameter concept with Java and C++, arguments modified in a function/method only remains that modification inside the function/method.
For this class, I simply added < handle in the head of class definition and it worked:
classdef tank < handle
properties
...
But I am not sure if that is the only solution, there might be better ways to do this. So I'll leave that question open, you are more than welcomed to post your opinion:D
In MATLAB, the call
p1.checkOri();
is equivalent to
checkOri(p1);
In both cases, the class method checkOri is called for the class of object p1, passing p1 as first argument to the function by value.
Because p1 is passed by value, any changes made to it inside the function are not seen by the object in the calling workspace. Therefore, one typically does
p1 = checkOri(p1);
This way, the object that was passed by value and modified inside the function is passed back out and assigned to the variable that held the original object.
If the method is written as follows:
function obj = checkOri(obj)
%...
end
then MATLAB will optimize the above function call such that no copy of the object is actually made. Note that both in the function declaration and in the function call, the input and output variable is the same.
As already discovered by OP, the above does not hold for handle classes, classes that inherit from handle. These classes act as if they are always passed by reference, and any change made to them in any workspace will be reflected in all other copies in other workspaces.
Also assigning to a member variable does not follow the above, such that
p1.value = 0;
modifies object p1.
For more information on the difference between value classes and handle classes see this other question.
I defined a class in one of my many MATLAB packages. To my suprise, I could not access a constant property of my class without importing the class definition. Even if it is a method of the class itself. Like so:
classdef TestClass
properties( Constant )
c=0;
end
methods
function obj = TestClass()
end
function getC(obj)
import test.TestClass;
disp(TestClass.c);
end
end
end
I just want to check whether I am doing something wrong here or is this the correct way to use constants in MATLAB.
Since you have placed TestClass inside a package Matlab needs to know where to look to find the definition for this class, even if it's a reference from within the class or function. An alternate to the above code could be:
function getC(obj)
disp(test.TestClass.c);
end
Alternately, if within a class, constant values can be accessed from the object itself.
function getC(obj)
disp(obj.c);
end
If neither of these is working for you, you may need to refresh the classdef for TestClass from memory. This will cause matlab to reload the constant value, which is pulled into Matlab when it first parses the classdef file to determine the structure of the class. This can be done using clear classes, however a warning that it will also clear all other classes, variables, and any breakpoints you have set.
If you want to see if this is necessary you can view the metaclass object to determine what Matlab "thinks" your class structure should be. You can do this using the following.
mc = ?test.TestClass;
mc.PropertyList
You may need to index into the property list to find the specific property you are interested in, but the thing you are looking for are the following fields.
Name: 'c'
Constant: 1
DefaultValue: 0
I wish to add a method called nansubset to the table class. Essentially it allows you to call T(r,c) where r and c are real positive integer vectors possibly containing NaN's.
Stubbed code for nansubset.m could be:
function T = nansubset(T, r, c)
T = T(r,c);
end
I am following the instructions here, which detail how to add a new method to the cell class. Basically, in a folder on my Matlab path, I create a folder called #table, and within this folder, create a file called nansubset.m.
I am getting the following problems:
>> tmpT = table(); nansubset(tmpT, 1, 1)
Undefined function 'nansubset' for input arguments of type 'table'.
and
>> doc #table/nansubset
Your search - #table/nansubset - did not match any documents.
However:
edit nansubset
and
edit #table/nansubset
both open the method file in my editor.
Further, I followed the instructions in the above link to add the plus method to the cell class and find that it works perfectly.
Can someone please explain to me how I can add this additional method to the table class?
With the release of Matlab R2012b (version 8), the class folder behavior changed (emphasis is mine):
In MATLAB Versions 5 through 7, class folders do not shadow other class folders having the same name, but residing in later path folders. Instead, the class the combination of methods from all class folders having the same name define the class. This is no longer true.
For backward compatibility, classes defined in class folders always take precedence over functions and scripts having the same name, even those that come before them on the path.
The combination of the two bold statements explains the behavior:
cell is a built-in Matlab function that predates the new OOP rules that returns an instance of its class. And before R2012b, adding methods to a class folder called #cell added the methods to the object returned from the cell function (which isn't defined with a classdef nor a class folder); this ability was retained for compatibility with legacy user code.
table was added after R2012b, is defined via a class folder, and is Sealed. Since it is Sealed, it cannot be subclassed. And with the new rules, any #table folder without an associated classdef file will not register as a class folder nor will its methods be composed into the existing class unless it is part of the legacy system (like cell).
I can see three workarounds listed in the order I think is best:
Have a function on the Matlab path, just like the one you have, and always call the "method" using function notation instead of dot notation. If you need more "methods" group them in a folder or package (if good namespacing is desired) on the path. Since table is a value class, this option doesn't seem bad.
Create a wrapper class like the one below. It is cumbersome but automatically encapsulates the additional functions.
classdef MyTable < handle
properties
tab;
end
methods
function mytab = MyTable(varargin)
mytab.tab = table(varargin{:});
end
function tabnan = nansubset(mytab,r,c)
tabnan = mytab.tab(r,c);
end
end
end
Create a local copy of [matlabroot,'\toolbox\matlab\datatypes\#table\*'] and add the methods directly. I can't think of any huge drawbacks to this per se, but it feels weird copying internals like this.
This question already has answers here:
Cannot assign property in method of struct
(2 answers)
Closed 7 years ago.
I'm studying swift and I'm trying to understand the difference between classes and struct, which as I understand it, is in these two points:
1) Classes are reference types, Whereas structures are value types. That means That When you pass an instance of a structure to a function, return an instance from a function, or assign the value of a variable That Refers to a structure to another variable, the instance is copied. In other words, structures exhibit pass-by-value behavior. Swift strings, arrays, and dictionaries are all Implemented as structures.
By contrast, class instances are passed by reference-no copy is taken.
2) Classes can be subclassed to add behavior; structures can not.
why these two errors?
depends on the structure ?, because a class does not give me the same error.
The reason you're getting those errors is that by default, the properties of a value type (i.e. a struct) cannot be modified from within its instance methods (e.g. your newRad() and plusOne() methods).
This question already has answers here:
How do properties work in Object Oriented MATLAB?
(3 answers)
Closed 3 years ago.
I've created a MATLAB class, something like:
classdef myclass
properties
x_array = [];
end
methods
function increment(obj,value)
obj.x_array = [obj.x_array ; value);
end
end
end
The problem is, the property x_array is never modified when I invoke the increment() function:
ex:
>>s = myclass
>>increment(s,5)
>>s.x_array
ans = []
I did some research, and I reached a conclusion that this is because of MATLAB using Lazy Copy for objects, making my class inherit the HANDLE class should have solved this, but it didn't, does anybody know why this is happening? And if extending the handle class is indeen the solution, isn't this the right way to do it:
classdef myclass < handle
or are there any extra steps?
This is similar to this question. In short all you should have to do is inherit from handle class.
Quick example
Contents of file myclass.m
classdef myclass<handle
properties
x_array = []
end
methods
function obj=increment(obj,val)
obj.x_array=[obj.x_array val];
end
end
end
Now from the Matlab command prompt, you can do the following
>> s=myclass;
>> s.increment(5)
>> s.increment(6)
>> s
s =
myclass handle
properties:
x_array: [5 6]
lists of methods, events, superclasses
There is an easier way. You only need to overwrite your initial instance s as follows:
s = increment(s,5);
More information in the documentation.
PS: While it is fine to use handle, the way copy function works is different and you should be careful about the way you use it. When you use handle, in fact you are making a new address/reference to an obj