Following this response, I set out to do something similar for datasets.
The plus
function mentioned in that answer, I got working fine for cell arrays by putting the plus.m in an @cell-folder.
However on trying the exact same (slightly different implementation) in a @dataset-folder, Matlab still gives an "undefined" error for both Dataset+Dataset
and plus(Dataset,Dataset)
.
What am I getting wrong?
Edit 1
In the current directory, there is the folder @dataset (same place as the working @cell folder), containing the file plus.m containing the code:
function C = plus(A,B)
% assuming same size, valid type, etc.
C = zeros(size(A));
for i = 1:size(A,2)
C(:,i) = A.(i) + B.(i);
end
end
In the command prompt, try:
ds = dataset({1,'a'},{2,'b'});
ds+ds
Matlab throws the error:
Undefined function 'plus' for input arguments of type 'dataset'.
Edit 2
Summary of workarounds
The short version is Daniels answer below. A write-up of the alternative options can be found here. In short, they present the options below (for overloading functions in general - not all solutions will allow the use of a standard operator such as '+' - here described for the present issue). None of them solves my problem, but could be used as workarounds.
- Define a new function that takes the desired class as arguments -- ala
dsplus(ds,ds)
- Define a new
plus
function, in which you catch and handle classes that you want to implement functionality for, and reroute others to the built-in. - Daniels suggestion.
- Make a new
mydataset
class inheriting from fromdataset
(classdef mySym < sym
) - Make a new
mydataset
class using composition (i.e. include the built-indataset
as a property of themydataset
class)
I would add a 6.th option: Leave Matlab and learn Python. An option that by the day looks increasingly appealing.