Overload mtimes for double*char
3 views (last 30 days)
Show older comments
I am working on a Matlab class that handles numbers/matrices with physical units. I would like the user to be able to write the input as (double)*(char), like this:
L = 10*’m’ % L is ten meters long
This operation calls the mtimes function for the double class. I can easily overload this function and make it work as I want with respect to (double, char)-input, but I still want it to work normally for (double, double)-input. Using * inside the mtimes function with (double, double)-input simply refers the function back to itself, resulting in an infinite regression.
Can I modify the original double mtimes function, or how do I go about this?
Thanks
0 Comments
Accepted Answer
James Tursa
on 30 Jan 2013
Edited: James Tursa
on 30 Jan 2013
The short answer is DON'T DO THIS! There are many built-in MATLAB functions that do (double)*(char) operations and depend on getting double results. If you overload this operation with your own you will end up breaking a bunch of built-in MATLAB functions. (And also potentially breaking any other code you happen to download from others, such as from the FEX).
To answer your question directly, however, you can sometimes use the function named 'builtin' to do things like this (but DON'T DO IT for your proposed case!)
More Answers (2)
Matt J
on 30 Jan 2013
You should define your own char class (e.g., mychar) with its own mtimes method.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!