Variable assignment of new class through mtimes works in command window but not in m-files.

I have overloaded the function mtimes to return a variable of my own class (called unit), when the inputs are of class (double, char). In the Matlab command window, I can use
2*kg' % ans is of class “unit”.
a = 2*kg % a is of class “unit”.
and it works perfectly. In m-files however, I can use
2*kg
but not assignments like
a = 2*kg
When I try, Matlab shuts down due to an “internal error”. I can use eval(‘a = 2*’’kg’’’) in m-files though. What is the problem here? Any idea of how to fix it? I know messing with the internal mtimes function is risky, but I really want this to work. Thanks.

1 Comment

No idea, but I guess that if I were in that case, I would overload subsasgn, subsref, subsindex, numel, and end, put breakpoints in all these methods, and checkout if it hits one of these breakpoints before crashing or not. In case it does, I would then step/step-inside progressively ..
I think though that what you did is quite risky .. I would have gone for an intermediary solution where I could perform operations like
>> x = unit(6, 'm') ; % or x = unit('m'); x(:) = 1:4 ; % =>subsasgn
>> t = unit(2, 's') ;
and then catch basic operators like mdivide, mdtimes, etc
>> v = x/t
v =
3 m/s

Answers (0)

This question is closed.

Products

Asked:

on 2 Feb 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!