MastHEad

 


Comparing MATLAB Data Types to k_units Data Type


 

This page compares some of the similarities and differences between a k_units data type and several MATLAB data types.

Overview of several MATLAB data types

To understand how the k_units data type works, it is helpful to first look at four MATLAB data types, namely double, char, struct, and table (the table data type became available in MATLAB 2013b). The first two data types are designed to hold numeric values and character strings, respectively. Please note that our use of the term "string" or "strings" implies the char data type and not the newer string data type. You can think of these two data types (double and char) as two different kinds of buckets, one holds numbers (double) and the other strings (char). A struct is a MATLAB data type which can hold many buckets (known as fields) where the various buckets can hold similar or different data types.  A MATLAB table is a data type designed to hold column-oriented tabular data and related meta-info. The table has Properties (think buckets) in which various types of meta-info are stored such as a Description, VariableNames (column names), VariableUnits (units associated to a column of data), and more. These Properties are very similar to the fields in a struct, except that in a table, the types of entities placed into the various Properties are restricted to certain data types for a given Property. The table data type views each column within itself like a variable and so each column must be of a certain data type, but the data type can vary from column to column. The MATLAB table data type is very powerful for organizing tabular-like data and its related information. One commonality between the struct and table is that if you do any math with either data type, you must extract the numeric data from the variable, do your math operation on the extracted numerical data, and then put the numeric result back into a struct or table (if you desire). Additionally, if you have a field in a struct that has unit labels (strings telling you the units of your data) or you have specified VariableUnits in a MATLAB table,  MATLAB has few, if any functions, that actually do anything with such units-related information.

Relative to the MATLAB data types described above, the Kornucopia k_units data type is most closely aligned to the MATLAB table. Their similarities and differences are described next.

Kornucopia k_units and MATLAB table  

  1. Both allow you to hold tabular data and related meta-information.

  2. The k_units data type fully supports Units and is supported by Kornucopia's fully-functional Units Engine.  The k_units data type stores the units as strings in a cell array within the Property of Units within the variable. You can easily and naturally work with a variety of units and unit systems and preferences. The Kornucopia Units Engine will check your calculations for units compatibility and it will do all the needed units conversions when and if you use mixed units.

  3. The k_units data type naturally works with all Kornucopia functions and many Kornucopia-overloaded MATLAB functions and operators. With the k_units data type, you generally do not have to constantly detach and re-attach your numerical data from your primary variable. In cases where a MATLAB function is not overloaded by Kornucopia, you will then need to supply just the numeric data portion of your variable to the MATLAB function via the ".Data" Property of the k_units data type.  

  4. MATLAB tables allow you to store, by column, various data types within its primary data section.  This means you can have a table with various columns holding different data types such as column of strings, a column of data type cell, a column of numeric data, and column of data type categorical, etc.  This allows you to stored in an orderly manner heterogenous information.

  5. Kornucopia functions like k_plot and k_displayOnFigure are well-suited to process BOTH the k_units data type and MATLAB tables (when the table's primary data Property holds numerical data only and thus meets the Kornucopia-compatible data type requirements).  It easy to create well formatted and clearly documented plots and figures, including automatic options to create axis labels, legends, and plot titles. This is all automated, yet flexible, due to the nature of the function k_plot and its ability to read the meta-info stored in Properties of k_units data type entities and MATLAB tables and then smartly place that information on the plot (including Units on the axes too!).