MATLAB: error when I try to access property of object - matlab

This is the error it returns:
You cannot get the 't' property of Planet.
Error in Problem4dot10 (line 12)
name=mercury.t(mercury);
I created getter functions to return properties of the class planet:
methods %for getter functions
function t=get.t(obj)
t=obj.t;
end
function r=get.r(obj)
r=obj.r;
end
function x=get.x(obj)
x=obj.x;
end
function y=get.y(obj)
y=obj.y;
end
function vx=get.vx(obj)
vx=obj.vx;
end
function vy=get.vy(obj)
vy=obj.vy;
end
function n=get.n(obj)
n=obj.n;
end
function n=get.Name(obj)
n=obj.Name;
end
end
And I called the getter from the file Problem4dot10.m:
mercury=Planet(1,0.002,0,2*pi,1,0,'Mercury');
mercury.sett(60);
name=mercury.t(mercury);
I looked at the documentation to try to figure out it out. What I'm trying to do is create a planet class that keeps track of the position of the planet, and also calculates where the planet is in the next time step. It then assumes that new state.

It seems to me that the t property is private. You can make it public using, e.g.:
properties(SetAccess = public, GetAccess = public)
t = 0;
end
or
properties(Access = public)
t = 0;
end
Also you don't need to pass mercury into t getter method. name=mercury.t; should be enough.

Related

Program in same OOP style as App Designer

I like the OO programming style that matlabs App Designer uses (or at least the way I'm using it). Now I'm wondering if I can use the same style in my "normal" matlab class.
What I have now:
classdef myClass
properties
myVar;
end
methods
function Main(obj)
obj.myVar = "a";
obj = DoSomething(obj);
disp(obj.myVar) % outputs "c"
end
function obj = DoSomething(obj)
if(obj.myVar == "a")
obj.myVar="c";
else
obj.myVar = "b";
end
end
end
end
Which can be called externally using:
myClassInst = myClass;
myClassInst.Main()
I would like to get rid of all the "obj = " in the classdef, as is possible in App Designer. So something that would look like this:
classdef myClass
properties
myVar;
end
methods
function Main(obj)
obj.myVar = "a";
DoSomething(obj); % Just call the function without "obj = "
disp(obj.myVar) % outputs "a" because I didn't overwrite obj
end
function DoSomething(obj)
if(obj.myVar == "a")
obj.myVar="c";
else
obj.myVar = "b";
end
end
end
end
The equivalent of this seems to work in App Designer. So it appears you can modify variables in a class (instance?) in App Designer, while also being able to access the modified variable without explicitly overwriting your old class instance.
I noticed App Designer has all methods an properties set to (Access = private), though I'm not sure that has anything to do with it. Of course if I set everything to private, then I can't access the Main() method from outside anymore.
So my question is, how can I program in "normal" matlab, the same way as is possible in App Designer?
EDIT:
The following works in App Designer (I left out the methods/properties for the GUI elements):
classdef tmp < matlab.apps.AppBase
properties (Access = private)
myVar; % Description
end
methods (Access = private)
function doSomething(app)
if app.myVar == "a"
app.myVar = "c";
else
app.myVar = "b";
end
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.myVar = "a";
doSomething(app);
disp(app.myVar); % outputs "c"
end
end
end
You definitely can! All you have to do is inherit from the handle class, as opposed to a value class which is the default for matlab. You can also define private and public methods as in other languages.
The only thing you have to do is:
classdef myclass < handle % this is how you inherit from base class
properties
public_property
end
properties (Access=private)
private_property
end
methods
function obj = myclass() % class constructor
...
end
function public_function()
...
end
end
methods (Access=private)
function private_function()
...
end
end
end
Now every time you pass an object of this class to a function, you are not passing it by value, you are passing by reference (as you might be used to from python) and modifying it's properties modifies them also in the original object.
You need to inherit (< at the top of the class) from a handle class
classdef myClass < handle
properties
var
end
methods
function obj = myClass( varargin )
% Constructor function, called automatically when object is created
end
function someFunction( obj )
obj.randomizeVar(); % Equivalent to randomizeVar( obj );
end
function randomizeVar( obj )
obj.var = rand();
end
end
end
See the documentation for the difference between a "handle" and "value" class:
A value class constructor returns an object that is associated with the variable to which it is assigned. If you reassign this variable, MATLABĀ® creates an independent copy of the original object. If you pass this variable to a function to modify it, the function must return the modified object as an output argument. For information on value-class behavior, see Avoid Unnecessary Copies of Data.
A handle class constructor returns a handle object that is a reference to the object created. You can assign the handle object to multiple variables or pass it to functions without causing MATLAB to make a copy of the original object. A function that modifies a handle object passed as an input argument does not need to return the object.
Moreover, if you edit matlab.apps.AppBase, the class which you app designer code inherits, you can see that the first line is
classdef AppBase < handle
So you are literally doing the same thing, without the AppBase middle-man.

Why isn't this object destroyed when it is cleared from the workspace?

I am working on a MATLAB class which stores an interface object created with tcpip and includes a callback function for the interface object to use, as per the following example:
classdef wsg50_mini2 < handle
properties
TCPIP
end
%PUBLIC METHODS
methods
%CONSTRUCTOR
function obj = wsg50_mini2(varargin)
fprintf('################# I am created #################\n')
obj.TCPIP = tcpip('localhost',1000);
obj.TCPIP.OutputBufferSize = 3000;
obj.TCPIP.InputBufferSize = 3000;
obj.TCPIP.ByteOrder = 'littleEndian';
obj.TCPIP.Timeout = 1;
%Setting up Callbackfunction
obj.TCPIP.BytesAvailableFcnMode = 'byte';
obj.TCPIP.BytesAvailableFcnCount = 1;
obj.TCPIP.BytesAvailableFcn = {#obj.TCP_Callback, obj};
end
end
%PRIVATE METHODS
methods (Access = private)
%DESTRUCTOR
function delete(obj)
fprintf('################# Hey I am called! #################\n')
instrreset
end
end
%STATIC METHODS
methods (Static)
%TCP Callback
%This function will be called if one Byte is available at the TCPIP
%buffer.
function TCP_Callback(tcpsocket,event,obj)
fprintf('Loading 1 Byte Data From Buffer.\n')
end
end
end
When I clear my class the variable will be cleaned from the workspace, but the delete destructor function is not called. Why not?
I realized, that my Instruments are still active in the Instrument Control app. If I delete my Instruments from there, my delete destructor is be called.
I think it is some strange behaviour of the tcpip-class.
It sounds very much like you have the wsg50 variable reference in your Instrument Control App class, in that case when you clear the variable from the workspace because it is still referenced elsewhere it is not deleted.
Lets look at a simple example:
classdef myClass < handle
properties
name = '';
end
methods
function delete ( obj )
fprintf ( '%s being deleted\n', obj.name );
end
end
end
Right lets run some code:
var = basicClass;
var.name = '123';
If we clear this variable then you can see the delete is called:
>> clear var
being deleted
If we re run this code and make a reference elsewhere:
var = basicClass;
var.name = '123';
otherReference.var = var;
Looking at both variables they are the same (as expected):
>> var
var =
myClass with properties:
name: '123'
>> otherReference.var
ans =
myClass with properties:
name: '123'
So what happens if we clear var and look at the other reference
clear var
otherReference.var.name
>> otherReference.var
ans =
myClass with properties:
name: '123'
We can see that the class variable is alive, as it should be as this is no difference than a class being an input to a function and when that function finishes the local copy of that variable is cleared from scope.
If we really want to delete the variable then you can do the following, where you explicitly run the destructor method:
var = basicClass;
var.name = '123';
otherReference.var = var;
delete(var);
otherReference.var.name
The delete line give us:
123 being deleted
While if we look at the otherReference.var we get:
Invalid or deleted object.
This will actually delete the variable and you will see that the otherReference is now a pointer to an invalid handle.
The problem is the line obj.TCPIP.BytesAvailableFcn = {#obj.TCP_Callback, obj};. The reference #obj.TCP_Callback creates a instance of the class inside the tcpip object, which is a property of the class itself.
the most easiest solution is to clear obj.TCPIP.BytesAvailableFcn = '' inside the destructor and call the destructor manualy. This is okayish if the code is only used by its creator.
To keep the object-oriented code intact, a Wrapper-Class works for the callback-function. The main file is updated with the Wrapper Class and a Listener as shown below. The Setup of the Callback is replaced with a combination of both of them. The Destructor needs then to call the destructors of the Wrapper (Which removes the instance of itself from the Callback) and of the Listener:
classdef wsg50_mini2 < handle
properties
TCPIP
TCPIPWrapper
LH
end
%PUBLIC METHODS
methods
%CONSTRUCTOR
function obj = wsg50_mini2(varargin)
fprintf('################# I am created #################\n')
% Create TCPIP Object
obj.TCPIP = tcpip('localhost',1000);
% Create TCPIP Wrapper
obj.TCPIPWrapper = TCPIPWrapper(obj.TCPIP)
% Add Listener
obj.LH = listener(obj.TCPIPWrapper,'BytesAvailableFcn',#obj.onBytes);
obj.TCPIP.OutputBufferSize = 3000;
obj.TCPIP.InputBufferSize = 3000;
obj.TCPIP.ByteOrder = 'littleEndian';
obj.TCPIP.Timeout = 1;
end
end
%PRIVATE METHODS
methods (Access = private)
%DESTRUCTOR
function delete(obj)
fprintf('################# Hey I am called! #################\n')
% Destroy Listener
delete(obj.LH);
% destroy Wrapper
delete(obj.TCPIPWrapper);
instrreset
end
end
%STATIC METHODS
methods (Private)
function onBytes(obj,~,~)
fprintf('Loading 1 Byte Data From Buffer.\n')
end
end
end
Additional the Wrapper looks as following:
classdef TCPIPWrapper < handle
properties(Access=private)
tcpip
end
events
BytesAvailableFcn
end
methods
% tcpipCallbackWrapper Constructor
function obj = tcpipCallbackWrapper(tcpip)
disp 'CONSTRUCTED tcpipCallbackWrapper'
% Keep a reference to the tcpip object
obj.tcpip = tcpip;
% Adding this listener will result in the destructor not being
% called automatically anymore; but luckily we have myClass
% which will explicitly call it for us
obj.tcpip.BytesAvailableFcnMode = 'byte';
obj.tcpip.BytesAvailableFcnCount = 1;
obj.tcpip.BytesAvailableFcn = #obj.bytesAvailableFcn;
end
% tcpipCallbackWrapper Destructor
function delete(obj)
disp 'DESTRUCTED tcpipCallbackWrapper'
% Overwrite the callback with a new empty one, removing the
% reference from the callback to our class instance, allowing
% the instance to truly be destroyed
obj.tcpip.BytesAvailableFcn = '';
end
end
methods (Access=private)
% Callback for BytesAvailableFcn
function bytesAvailableFcn(obj,~,~)
notify(obj, 'BytesAvailableFcn');
end
end
end

How do I call get method in matlab?

classdef Untitled
enumeration
M('monday','first day')
T('tuesday','second day')
end
properties(SetAccess=private)
name
description
end
methods
%constructor
function obj = Untitled(name, description)
obj.name = name
obj.description = description
end
%getter
function name = get.name(obj)
end
%getter
function description = get.description(obj)
end
end
end
How can I call get method in command window after I make instance of Untitled? I am new to matlab and not sure is that even possible, cause I read that getter and setter cant be called directly?
There are a couple of problems with the code you provided. First, an enumeration class is a special type of class in Matlab. You may want to read more about enumeration classes in general in Matlab, and about their restrictions.
If I assume you are not trying to create an enumeration class, and remove that part of your function, the next problem is that your getters don't do anything. You should at least do:
function name = get.name(obj)
name = obj.name
end
However, if all you're going to do is return the value of the property, you don't even need to create a get function. Here's some code which works:
classdef Untitled
properties(SetAccess=private)
name
description
end
methods
%constructor
function obj = Untitled(name, description)
obj.name = name;
obj.description = description;
end
end
end
Then you can do:
myobj = Untitled('myname','mydesc');
myobj.name
myobj.description

object oriented MATLAB: static method from parent can't set protected property from child

Ok, so I have a parent class MOM with a bunch of properties with the SetAccess attribute set to protected. It has a special static method setProperty for setting its properties:
classdef MOM < handle
properties (SetAccess = 'protected')
age;
occupation;
num_kids;
MOMmusthaves ={'age','occupation','num_kids'};
end
methods
function obj = MOM(varargin)
if iscell(varargin{1,1})
varargin =varargin{1,1};
end
inputstrings=varargin(cellfun(#isstring,varargin)); % get strings out of the inputs
if(isempty(setxor(intersect(inputstrings,obj.MOMmusthaves),obj.MOMmusthaves)))
% if all proper inputs are entered, initialize your object
for pp =1:length(obj.MOMmusthaves)
obj.setProperty(obj,varargin,obj.MOMmusthaves{pp});%find each input in the vararagins, and set it
end
else
display('Hey! These inputs are wrong. type help MOM for an example. Make sure you inlcude inputs for all of the following:'); obj.MOMmusthaves
return;
end
end
end
methods (Static)
function setProperty(obj,varargs,xx)
eval(sprintf('obj.%s = varargs{find(strcmp(''%s'',varargs))+1};',xx,xx));
end
end
end
Then I have a child object KID, which has a couple more properties, also SetAccess protected. When I try to use MOM's static method to set a KID property inside the KID's constructor I get an error :(
The error says:
You cannot set the read-only property 'allowance' of KID.
Basically, it seems like KID doesn't think that it can use MOM's static method as its own (so didn't inherit it properly).
MY QUESTION IS:
Is there any way I can make the static method be recycled and usable to KID for its own protected properties?
JUST FYI, here's something like the KID code;
classdef KID < MOM
properties (SetAccess = 'protected')
gender;
allowance;
favoritecandy;
KIDmusthaves ={'gender','allowance','favoritecandy'};
end
methods
function obj = KID(varargin)
obj = obj#MOM(varargin(:)); % Special construct for instantiating the superclass
inputstrings=varargin(cellfun(#isstring,varargin)); % get strings out of the inputs.
if(isempty(setxor(intersect(inputstrings,obj.KIDmusthaves),obj.KIDmusthaves)))
% if all proper inputs are entered, initialize your object
for pp =1:length(obj.KIDmusthaves)
%find each input in the vararagins, and set it
obj.setProperty(obj,varargin,obj.KIDmusthaves{pp});
end
else
display('please use correct input format. Make sure you include inputs for all of the following:');
obj.KIDmusthaves
return;
end
end
end
end
I'm not really sure what the exact source of the error is; although, I think it may be due to the mutation of the handle object being hidden by the eval.
Regardless, if I understand the intended usage of setProperty, I think it may be easiest to write the function in a non-static form using dot notation (similar to dynamic field names with structs):
methods
function [] = setProperty(obj,musthaves,varargin)
keys = varargin(1:2:end);
values = varargin(2:2:end);
for pp =1:length(keys)
key = keys{k};
if any(strcmp(musthaves,key))
obj.(key) = values{pp};
end
end
end
end
where musthaves is any cell array of property strings
You could also have musthaves be a string that indicates the obj property holding the property list:
methods
function [] = setProperty(obj,musthaves,varargin)
keys = varargin(1:2:end);
values = varargin(2:2:end);
for pp =1:length(keys)
key = keys{k};
if any(strcmp(obj.(musthaves),key))
obj.(key) = values{pp};
end
end
end
end

Calling class object from a function without insert it to the function in MATLAB

We have this code in 'Reconstruction the subclass object from a saved struct' from MATLAB OOP documentation.
classdef MySuper
% Superclass definition
properties
X
Y
end
methods
function S = saveobj(obj)
% Save property values in struct
% Return struct for save function to write to MAT-file
S.PointX = obj.X;
S.PointY = obj.Y;
end
function obj = reload(obj,S)
% Method used to assign values from struct to properties
% Called by loadobj and subclass
obj.X = S.PointX;
obj.Y = S.PointY;
end
end
methods (Static)
function obj = loadobj(S)
% Constructs a MySuper object
% loadobj used when a superclass object is saved directly
% Calls reload to assign property values retrived from struct
% loadobj must be Static so it can be called without object
obj = MySuper;
obj = reload(obj,S);
end
end
end
I have a question about obj = MySuper. What is purpose of this line? How we can call MySuper object from this function without insert any object to loadobj function?
You first question is: What is the purpose of the obj = MySuper; line?
The answer is that the obj = MySuper; line initiates the variable obj as an element of the class MySuper. Non-static functions in a class will only run if the first input parameter is an instance of the class, so if obj is not initiated as a MySuper-object, then matlab will look for other functions called reload to run, and if none is found give you an error.
For your second question, I am not 100% sure what you mean. But I hope one of the following points will answer your question:
If you want to make a function that relates to a class, but not to a specific instance of the class, you can make a static function, these can take any input (also (if you want it that way) no input at all) - that is they don't need to have a first input parameter of the specific class.
To run a static function, use the class name followed by a dot and then the function name, so here you would type MySuper.loadobj(S) to run the function with the parameter S.
I would suggest that you try this out with the given example to better get to know the way oop works in matlab, you may for example try:
S.PointX = 1;
S.PointY = 2;
obj = MySuper.loadobj(S)
I hope this answers your questions.