Comments on: When to Create Classes in MATLAB https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/?s_tid=feedtopost Loren Shure is interested in the design of the MATLAB language. She is an application engineer and writes here about MATLAB programming and related topics. Sun, 31 Jul 2016 18:58:02 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Daniel Carleton https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30238 Tue, 28 Apr 2009 00:09:50 +0000 https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30238 Nausheen,

It’s also worth noting that when I use a value type for MyList I get extremely poor performance as you predicted.

>> list_compare
Elapsed time is 20.858983 seconds.
Elapsed time is 0.000622 seconds.

It’s very large compared with inheriting from handle, but inheriting from handle is still inexplicably slow as per my post above.

Thanks,

– Daniel

]]>
By: Daniel Carleton https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30231 Sun, 26 Apr 2009 04:05:55 +0000 https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30231 Hello Nausheen,

In my post I think I included the wrong code. My list is indeed a subclass of handle, and shows the performance characteristics from my last post:

>> list_compare
Elapsed time is 0.083423 seconds.
Elapsed time is 0.000395 seconds.

Any other ideas? Our application is severely bottlenecked by poor list performance. I wrote a detailed post below.

ht​tp://dacc.exaptic.com/2009/01/list-class-in-matlab-oo-overhead-pass-by-strategies/

Thanks,

– Daniel

]]>
By: Loren https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30215 Mon, 20 Apr 2009 12:14:45 +0000 https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30215 Francisco-

It is intended behavior that package contents are not automatically imported or on the path. There is no intention to not allow users to write methods for builtin classes, though, if you override one of the builtin methods, some functionality may no longer work as intended.

I recommend you read the documentation for more information about how to work with packages.

–Loren

]]>
By: Francisco Valverde https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30214 Mon, 20 Apr 2009 10:51:08 +0000 https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30214 Hello Loren,

Regarding #23, I would like to subclass ‘double’ to a +mypackage/@double to redefine operators such as ctranspose and mtimes.

But unless the @double dir lies directly in one of the dirs in *path*, Matlab seems unable to find it. E.g. ‘which mtimes’ will not find +mypagkage/@double/mtimes.m even if it exists and is declared in +mypackage/@double/double.m

Is that intended behaviour to prevent the users’ monkeying too much with builtin classes?

TIA
F.Valverde

]]>
By: Jiro https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30203 Thu, 09 Apr 2009 16:53:25 +0000 https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30203 Rainer,

If “anothertype” is a user-defined MATLAB class, you can use the InferiorClasses attribute on your “mytype” to make it have higher precedence than “anothertype”.

]]>
By: Rainer https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30202 Thu, 09 Apr 2009 02:09:20 +0000 https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30202 Hello Loren,

I want a new class (mytype) to seamlessly interface with existing classes (builtin, e.g. “double” and non-builtin datatypes from existing other 3rd party toolboxes, e.g. “anothertype”).
This seems easy to achieve when the first argument of a function is my new datatype e.g.

a=mytype(10);
b=anothertype(12);
a+b % calls @mytype/plus
b+a % calls @anothertype/plus — problem here

The last line is undesirable for me, since I would like to overload the plus function in such a way that @mytype/plus gets called if ANY of its arguments is of type “mytype”.
If I am not starting to modify the code of @anothertype/plus (which I do not want to touch), I did not see a possibility to influence the behaviour.

Is there any possibility to influence which function gets called for which combination of datatypes?

Thanks for any help in clarifying this,

Rainer

]]>
By: Nausheen Moulana https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30047 Thu, 19 Feb 2009 00:01:20 +0000 https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30047 Hi Peter:

Here are some options you have to redefine operations on integers only.

1) You can wrap on overflow by adding your own arithmetic operators for example, a plus method for the uint8 data type. You can do so by creating and adding a new @uint8 directory to a directory your MATLAB path (..\myDir\@uint8). In this directory you can add a plus method which contains the following code:

function result = plus(A,B)
result = uint8(mod(double(A)+B,256));

The above is just an example, it doesn’t account for error handling and performance.

2) You can subclass from a built-in integer type and redefine the operations you are interested in.

It’s generally good to associate different behaviors with different classes. By using the second approach, you can use uint8 for the default saturate behavior and your own myuint8 class for wrap behavior.

Nausheen

]]>
By: Peter Noronha https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30035 Wed, 11 Feb 2009 19:46:18 +0000 https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-30035 Loren,

In this post you wrote:
“Another reason I may want to create a new type is to extend or redefine operations on an existing type. For example, I may require integer arithmetic to wrap as opposed to saturate (which is what MATLAB does) on overflow.”

I am interested in doing exactly this but don’t know how to extend or redefine the math operators. If this particular example has been implemented somewhere, could you point me to it?

I would like to set this (wrap on overflow) as the default behavior for math operators within my MATLAB session for all integer data types without affecting the behavior of these operators for any other data type. I wonder if this behavioral setting should be something that should be available under MATLAB > Preferences…

Thanks,

Peter

]]>
By: Jennifer Black https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-29978 Thu, 22 Jan 2009 19:46:58 +0000 https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-29978 JP,

The code you are referring to with the schema.m files is part of experimental code that is not part of the official MATLAB Class System. At present, we do not provide support for classes in nested @-directories. However, you can make use of packages to create nesting relationships.

To create a package directory, use the + symbol as a prefix for the directory name. Then, within that directory you can place @-directories containing class definitions. You can also place additional (nested) package directories.

For example, given the following directories:

+pkg1/@Class1
+pkg2/+pkg2/@Class2

Class1 is a class contained within pkg1, and can be instantiated using the syntax:

>> obj = pkg1.Class1;

Class2 is contained within the package pkg2, which is itself nested within pkg1. To create an instance, use the following syntax:

>> obj = pkg1.pkg2.Class2;

Hope this helps –

Jennifer

]]>
By: J P Barnard https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-29970 Thu, 22 Jan 2009 07:30:28 +0000 https://blogs.mathworks.com/loren/2008/08/18/when-to-create-classes-in-matlab/#comment-29970 Hello Loren

Question on nested classes, using the new class syntax: I have noticed such file structures in the MATLAB toolboxes, but in the documentation it is stated that an @ folder with new class syntax will shadow all @folders under it, making them invisible even to the top level class. I have also noticed some schema.m in at the non-@ folders, containing these nested @ folders. What is the trick, because I cannot get that nesting to work and the documentation is not clear on this aspect?

Cheers

JP

]]>