How to make an object accessible within its class, in Matlab? - matlab

In this class how can I have fig object with its own relevant properties available in its class, for example in my_function;
classdef Test
properties
a
b
end
methods
function obj = Test(a, b)
obj.a = a;
obj.b = b;
end
function [] = my_function(obj)
fig.Name %%% here fig object is needed
disp('done!')
end
function [fig] = my_figure(obj)
fig = figure();
end
end
end

You need to store fig as a property of your class and then from within my_function, you'll be able to access the fig property of the current instance. As a side note, if you'd like to be able to pass your class instance around by reference, you'll want to subclass MATLAB's handle class:
classdef Test < handle
properties
fig % Setup a property to hold the handle to the figure
a
b
end
methods
function obj = Test(a, b)
obj.a = a;
obj.b = b;
end
function [] = my_function(obj)
% Access and modify the figure handle as needed
obj.fig.Name = 'Name';
disp('done!')
end
function [fig] = my_figure(obj)
fig = figure();
% Store the handle in the "fig" property of the class
obj.fig = fig;
end
end
end

Related

Recursion Error in MATLAB

I've created two fairly simple classes in MATLAB, and I have a problem with running this.
classdef A
properties
a
end
methods
function obj = A(a)
obj.a = a;
end
end
end
classdef B
properties
b
end
methods
function obj = B(b)
obj.b = b;
end
end
end
Then, I wrote in the command window:
x = A(1);
y = B(x);
and I got the following error:
Maximum limit of 500 reached.
Why do I get this? And how should I fix it?

Use one set method for multiple properties MATLAB

I have several properties that use essentially the same set method:
classdef MyClass
properties
A
B
end
methods
function mc = MyClass(a,b) % Constructor
mc.A = a;
mc.B = b;
end
function mc = set.A(mc, a) % setter for A
if a > 5
mc.A = a;
else
error('A should be larger than 5');
end
end
function mc = set.B(mc, b) %setter for B
if b > 5
mc.B = b;
else
error('B should be larger than 5');
end
end
end
end
Is there a way to use only one set function for variables A and B? (Please note that the error function use the property names as strings.)
Is it suggested to use only one set function? What are the possible drawbacks of using one set function?
The only real way is to extract the common code to another function, and call it from the setters:
classdef MyClass
properties
A
B
end%public properties
methods
function mc = MyClass(a,b) % Constructor
mc.A = a;
mc.B = b;
end
function mc = set.A(mc, value) % setter for A
mc = mc.commonSetter(value, 'A');
end
function mc = set.B(mc, value) %setter for B
mc = mc.commonSetter(value, 'B');
end
end%public methods
methods(protected = true)
function mc = commonSetter(mc, property, value)
if value <= 5;
error([property ' should be less than 5');
end
mc.(property) = value;
end%commonSetter()
end%protected methods
end%classdef

Output of class function in Matlab

I'm working with classes a long time ago but yet I couldn't get one thing that how to OUTPUT from a function/Constructor of like a function. I have seen multiple examples but coulnd't clear the point. Here I'v a simple example of myFunc outputting an array, and same function in class, How to output from class like a function.
How to take output from any function of class just like a function?
myFunc:
function M=myFunc(n)
[M]=[];
i=0;
for ii=1:n
M(ii)=i;
%% Counter
i=i+4;
end
end
MyClass:
classdef myClass
properties (Access=private)
n
M
i
end
methods
function obj = myClass(n)
obj.n = n;
end
function myFunc(obj)
for ii=1:obj.n
obj.M(ii)=obj.i;
%% Counter
obj.i=obj.i+4;
end
end
end
end
**EDIT 1:**
classdef myClass
properties (Access=private)
n
M
i
end
methods
function obj = myClass(n)
obj.n = n;
end
function M = myFunc(obj)
for ii=1:obj.n
obj.M(ii)=obj.i;
%% Counter
obj.i=obj.i+4;
end
M = obj.M;
end
end
end
A method works just like a normal function, except that the first input of a non-static method is always expected to be a class instance
You call the method defined as
methods
function [out1, out2] = fcnName(object, in1, in2)
% do stuff here (assign outputs!)
end
end
like so:
[out1, out2] = object.fcnName(in1,in2)
or
[out1, out2] = fcnName(object,in1,in2)
Applied to your example:
methods
function M = myFunc(obj)
for ii=1:obj.n
obj.M(ii)=obj.i;
%% Counter
obj.i=obj.i+4;
end
M = obj.M;
end
end
you call myFunc as
obj = myClass(3);
M = myFunc(obj);

changing class properties with its methods

I'd like to change my class properties with a method defined for that class:
classdef triangle<handle
properties
a
h
end
methods
function obj = triangle()
obj;
end
function obj = setProps(obj, a, h)
obj.a = a;
obj.a = h;
end
end
end
Calling:
t = triangle();
t.setProps(a, h);
It's not working at all - I get this error:
The class 'handle' is not a super-class of class 'triangle', as required to invoke a super-class constructor or method.
Error in triangle (line 13)
function obj = triangle()
I'm using matlab 2012a. My code is based on this example: link
Try clear before doing this. It is possible that you've overwritten handle with something. Otherwise, this works for me on Matlab 2012a:
clear;
a = 'hello';
h = 1;
t = triangle();
t.setProps(a, h);

Use a property of a classdef in another .m file?

Here is my code:
f.m:
classdef f < handle
properties (Access = public)
functionString = '';
x;
end
methods
function obj = f
if nargin == 0
syms s;
obj.x = input('Enter your function: ');
obj.functionString = ilaplace(obj.x);
end
end
function value = subsref(obj, a)
t = a.subs{:};
value = eval(obj.functionString);
end
function display(obj)
end
end
end
test.m:
syms s t;
[n d] = numden(f.x); % Here I want to use x, which is the user input, How can I do such thing?
zeros = solve(n);
poles = solve(d);
disp('The Poles:');
disp(poles);
disp('The Zeros:');
disp(zeros);
disp('The Result:');
disp(z(t));
disp('The Initial Value:');
disp(z(0));
disp('The Final Value:');
disp(z(Inf));
When I type test in the command window, it tells me the following:
>> test
??? The property 'x' in class 'f' must be accessed from a class instance because it
is not a Constant property.
As Alex points out, you need an instance of f to access the member property x, like so:
myf = f();
f.x
You do not need an accessor method to get at x as it is defined as a public property. If you chose to make x private, then you'd need an accessor method something like this:
function x = getX( obj )
x = obj.x;
end