matlab-class

How can I validate a function handle as an input argument?

人走茶凉 提交于 2019-12-04 12:25:15
I have a class that has a function handle as one of its properties . classdef MyClass properties hfun %function handle end methods function obj = Myclass(hfun,...) %PROBLEM: validate that the input argument hfun is the right kind of function if ~isa(hfun,'function_handle') || nargin(hfun)~=1 || nargout(hfun)~=1 error('hfun must be a function handle with 1 input and 1 output'); end obj.hfun = hfun; end end end I'd like to make sure that the input argument hfun is a function handle with 1 input and 1 output, otherwise it should error. If I could get even more specific I'd like this function to

How can I tell how much memory a handle object uses in matlab

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 23:28:00
If I declare an object to be a subclass of handle classdef obj < handle my object is now essentially a "pointer" to some memory somewhere. How do I find out how much memory my object is using up? For example, say I have a class foo with a field bar classdef foo < handle properties bar = randn(1000); end bar takes up 8 megabytes (8 bytes * 1 million numbers) but if I type obj = foo(); whos('obj'); I get Name Size Bytes Class Attributes obj 1x1 60 foo How do I find out how much total memory obj points to? As a hack, convert it to a struct and see how much space that takes up. I think that will

Best way to organize MATLAB classes? [closed]

两盒软妹~` 提交于 2019-12-03 19:11:33
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . MATLAB has two ways of organizing classes: @-directories: @ClassName\ ClassName.m Method1.m Method2.m Single files: ClassName.m: classdef ClassName methods % all methods included here end end The first style existed before the new classdef syntax, but seems to be a more

MATLAB - create reference (handle?) to variable

假装没事ソ 提交于 2019-12-03 06:17:14
问题 Suppose I have the following class: classdef myClass < handle properties A = 1 end methods function obj = myClass(val) obj.A = val; end end end Say I instantiate an instance of this class, then manipulate it slightly and then copy it. As it's a handle class, the "copy" is really just another instance of the same object: >> q = myClass(10); >> q.A = 15; >> w = q; >> disp(w.A) 15 But I would like to watch A without needing to instantiate myClass. Naively doing >> value = w.A doesn't work, since

MATLAB - create reference (handle?) to variable

流过昼夜 提交于 2019-12-02 20:52:11
Suppose I have the following class: classdef myClass < handle properties A = 1 end methods function obj = myClass(val) obj.A = val; end end end Say I instantiate an instance of this class, then manipulate it slightly and then copy it. As it's a handle class, the "copy" is really just another instance of the same object: >> q = myClass(10); >> q.A = 15; >> w = q; >> disp(w.A) 15 But I would like to watch A without needing to instantiate myClass. Naively doing >> value = w.A doesn't work, since this just copies the value; changning w.A later on will not change value . Is there a way to provide a

Why do I get a “Too many input arguments” error when passing in proper parameters?

限于喜欢 提交于 2019-12-01 11:26:58
Why am I getting an error: ??? Error using ==> ensureCellType Too many input arguments. Error in ==> usage_dynamicVariableNaming at 11 result = dataHolder.ensureCellType(str); when I am passing in right number of parameters? % USAGE: clear all; clc; elementNames = {'area_12345[<>]6789', 'apollo123', 'guruX', 'ok'}; elementTypes = {'string', 'specialChar', 'int', 'float'}; elementValues = {'charlie', 'vvv', '09', '123.321'}; dataHolder = dynamicVariableNaming; str = 'test'; result = dataHolder.ensureCellType(str); %% CLASS classdef dynamicVariableNaming %HELLO Summary of this class goes here %

MATLAB CLASSES getter and setters

浪子不回头ぞ 提交于 2019-12-01 06:16:43
I come from a Java background. I am having issues with classes in Matlab particularly getters and setters. getting a message saying conflict between handle and value class I'm a little lost with what to do so any help for lack of a better word will be helpful. classdef Person properties(Access = private) name; age; end methods % class constructor function obj = Person(age,name) obj.age = age; obj.name = name; end %getters function name = get.name(obj) end function age = get.age(obj) end %setters function value = set.name(obj,name) end function value = set.age(obj,age) end end end

MATLAB CLASSES getter and setters

笑着哭i 提交于 2019-12-01 04:59:14
问题 I come from a Java background. I am having issues with classes in Matlab particularly getters and setters. getting a message saying conflict between handle and value class I'm a little lost with what to do so any help for lack of a better word will be helpful. classdef Person properties(Access = private) name; age; end methods % class constructor function obj = Person(age,name) obj.age = age; obj.name = name; end %getters function name = get.name(obj) end function age = get.age(obj) end

How do I create an array of abstract class objects in MATLAB?

天涯浪子 提交于 2019-11-30 05:11:38
As an example, suppose I have created an abstract class called Shape and two subclasses called Circle and Rectangle that both implement an (abstract) method called Draw . I would like to be able to create a number of Circle and Rectangle objects, store them in an array and call Draw on each array object by iterating through the array. I have tried something like the following: Shape.m: classdef (Abstract) Shape < handle methods (Abstract) Draw(obj); end end Circle.m: classdef Circle < Shape methods function obj = Draw(obj) disp('This is a circle'); end end end Rectangle.m: classdef Rectangle <

Constants in MATLAB

谁都会走 提交于 2019-11-28 17:47:48
I've come into ownership of a bunch of MATLAB code and have noticed a bunch of "magic numbers" scattered about the code. Typically, I like to make those constants in languages like C, Ruby, PHP, etc. When Googling this problem, I found that the "official" way of having constants is to define functions that return the constant value. Seems kludgey, especially because MATLAB can be finicky when allowing more than one function per file. Is this really the best option? I'm tempted to use / make something like the C Preprocessor to do this for me. (I found that something called mpp was made by