MastHEad

 


Overloaded MATLAB Functions


 

Kornucopia has been designed and coded so that variables and other entities of data type k_units will work naturally with many MATLAB functions and syntax.  This means the following type of commands can be used:

 

k_unitsVariables( 'Hz, in, s' );
fApplied = 10*Hz;
yAmp = 20*in;
t = k_vectorByPoints(0*s,1*s, 1000);
y = yAmp*sin(2*pi*fApplied*t);
curve = [t,y];

 

In the above syntax example, the following were variables of data type k_units:

In the commands shown above, the *, sin, pi, and concatenation braces,  [ ], were standard MATLAB operators, functions, or a constant.

MATLAB is able to process the commands shown above because Kornucopia overloaded these MATLAB functions and operators so that they can recognize and properly execute when entities of data type k_units are supplied to them. The user does not need to do anything special, the overloading is all handled by the installation of Kornucopia software on your computer.  Before we review the list of MATLAB functions overloaded by Kornucopia, it is instructive to provide some further explanation of overloading.

How overloading works - A simplistic description

When any function is used in a command, MATLAB looks to see if it knows how to handle the data types that are supplied to the function through its arguments. If a data type is unknown to the function, then MATLAB looks to see if the unknown (to the function) data type has an overloaded definition of the function. If it does, then MATLAB passes all the input arguments to that data type's class for it's version of the function to be executed. In essence, what is occurring, is that only when a function does not know how to process a particular data type, then the function's operation is passed to the class of that data type. Otherwise the normal function's code is executed as expected.

Kornucopia has overloaded over 80 MATLAB functions and operators so that the user experiences an efficient and natural MATLAB feel to their work when using the k_units data type. Provided in the listings that follow are all the overloads provided by Kornucopia.  It is noted with MATLAB's hundreds of functions, there will be cases where there is no Kornucopia overload for a given MATLAB function (or user function). These cases are discussed after the listing below (Click here for that specific section).

Kornucopia overloads of MATLAB functions and operators

Kornucopia overloads over 80 MATLAB functions and operators. This means that for those functions and operators with overloads, you can supply to them entities of data type k_units and they will simply work, very much like they would if you supplied normal MATLAB double data type (or similar).  You do NOT need to use the  A.Data syntax with these functions and operators.

Arithmetic math operators and functions

Relational operators

Trigonometry

Logs and Exponents

Complex numbers and angles

Descriptive statistics

Reshaping arrays

Is something

Matrix operations

Display functions

Please note the following:

 txt = num2str(myVar, '%.5k');

Syntax operators and functions

Kornucopia variations on MATLAB function behavior

Most Kornucopia overloaded MATLAB functions behave similar to their native MATLAB function behavior. However, due to the column-oriented nature of the k_units data type, there are certain conditions where some overloaded functions will behave differently from the native MATLAB behavior. These cases are described here.

MATLAB behavior for functions that do NOT have k_units overload

Since MATLAB has hundreds of functions, there will be cases where there is no Kornucopia overload for a given MATLAB function (or user function). If you need to use a function that has not been overloaded, simply supply just the numerical data from the k_units data type via A.Data where A is the name of your variable (this is shown in the code box below).

If a k_units data type is supplied to a MATLAB function that Kornucopia has NOT overloaded, then the user will likely see a MATLAB error of one of two likely forms:

  1. An error stating something like 
    "Undefined function 'XXX' for input arguments of type 'k_units'"
    (where XXX is the MATLAB or user function that was being executed).

  2. A MATLAB error dump that might have several levels in it which there may ultimately be an error statement like that above or some other Kornucopia error statement, typically referencing that a variable was indexed improperly or some other type of error.

In the first case, it definitely means that there is no Kornucopia overload for the MATLAB function in question. In the second case, it may mean this or it may mean that you simply have a syntax error.  In the event that you believe the issue is lack of an overloaded function, a simple solution to the problem is to feed the MATLAB function just the primary data from the k_units variable by using A.Data syntax on the k_units variable (where A is assumed here to be your variable name). Doing this results in just a normal array of data (just numbers) being fed as the argument into the MATLAB function and likely a result of data type double being returned by the MATLAB function. Below is a simple demonstration where the variable myStuff is of data type k_units.

 

yFfilt = filter(b,a,y); % If y is k_units data type, this will cause error.
yFilt = filter(b,a,y.Data); % This feeds just numerical data from y

 

One note on the example above: If you are trying to perform DSP filtering of your data, look at the function k_filter, it is likely to do what you want more directly.