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);
Related
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?
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'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);
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