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?
Related
In the matlab, I defined a class and instantiated the class in another script, but I got different values in and out of the method. My matlab code are shown bellow:
test_add.m
classdef test_add
properties
a
b
end
methods
function obj = test_add(a, b)
obj.a = a;
obj.b = b;
end
function c = add_1(obj)
c = obj.a + 1;
end
function inter(obj, t)
for i = 1:t
obj.a = obj.add_1();
end
fprintf('In the method:\n');
fprintf('a = %d\n',obj.a);
fprintf('b = %d\n',obj.b);
disp('=======================');
end
end
end
main.m
tt = test_add(1,2);
tt.inter(3);
fprintf('Out of the method:\n');
fprintf('a = %d\n',tt.a);
fprintf('b = %d\n',tt.b);
output:
In the method:
a = 4
b = 2
=======================
Out of the method:
a = 1
b = 2
In Matlab there are two type of classes: handle class and Value class. If you said nothing you get the Value class. Most of the OO languages out there are using handle class semantic.
So, you have two options:
Change you class to handle class by inheriting from handle
classdef test_add < handle
Stay with Value class and change your inter function to return obj.
But then, in main call obj=tt.inter(3) to get the updated object.
function obj = inter(obj, t)
for i = 1:t
obj.a = obj.add_1();
end
fprintf('In the method:\n');
fprintf('a = %d\n',obj.a);
fprintf('b = %d\n',obj.b);
disp('=======================');
end
It's a problem of value class vs handle class, the solution is:
test_add.m
classdef test_add < handle
...
end
Then, the output is:
In the method:
a = 4
b = 2
=======================
Out of the method:
a = 4
b = 2
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
The very first one in this documentation:
http://www.mathworks.com/help/matlab/matlab_oop/getting-familiar-with-classes.html
The class is:
classdef BasicClass
properties
Value
end
methods
function r = roundOff(obj)
r = round([obj.Value],2);
end
function r = multiplyBy(obj,n)
r = [obj.Value] * n;
end
end
end
When I run it this way
a = BasicClass
a.Value = pi/3;
It works fine and does what it should but this piece of code
a = BasicClass(pi/3);
Gives the following error:
"Error using round
Too many input arguments."
What does it mean? (I'm using R2014a) Is it stupid to use oop in Matlab? LOL
Your error message doesn't look correct compared with the code, whichever your missing a class constructor (as is mentioned at the half way down the help link):
classdef BasicClass
properties
Value
end
methods
% Class constructor -> which you can pass pi/3 into.
function obj = BasicClass ( varargin )
if nargin == 1
obj.Value = varargin{1};
end
end
% Your Methods
function r = roundOff(obj)
r = round([obj.Value],2);
end
function r = multiplyBy(obj,n)
r = [obj.Value] * n;
end
end
end
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);
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);