This page describes, in detail, how Kornucopia's data type k_units works within the MATLAB environment.
Sub-sections on this help page:
Putting it all together - Example of creating a decaying sinusoidal velocity vs time curve
Using a MATLAB function that does not support k_units data type
As noted in the previous help sections (Kornucopia Introduction & Features and Overview of Kornucopia), Kornucopia's k_units data type is designed to hold scalar or tabular-oriented data, including units and other meta-information. The k_units data type strives, whenever possible, to keep your numeric data, its units, and any related meta-info intact throughout your calculations. This means that the k_units data type is designed to allow you to use it naturally in MATLAB's mathematical operations, with Kornucopia functions, and with many MATLAB functions without the need to constantly separate and re-attach the numerical data from the rest of the data type (like is the case with traditional MATLAB work-flows using MATLAB table, cell, and/or struct data types). A significant difference of the k_units data type over other data types is that units stored in a k_units data type are "real units", not just tags. Through Kornucopia's full-featured Units Engine, Kornucopia will know how to identify and calculate with these units. This means you have the freedom to work with units of all sorts.
In brief, Kornucopia allows users to associate units like 'm', 'N', or 'lbf' to their data in a column-wise manner. Units can also be combined units like 'N/mm', 'lbf*in/s', 'MPa^0.5', or even something like 'in^3/(snail*s)'. The Kornucopia Units Engine is also able to readily handle mixed units like 'lbf/mm' or something crazy like '(G^2/Hz)*kg*mile/day'. When data associated with units via the k_units data type is supplied to Kornucopia functions, or is supplied to any of the overloaded MATLAB functions and operators, the functions will recognize the units and properly utilize them, including checking for units compatibility and automatically doing units conversions as needed so that you obtain accurate and clearly denoted results. Another handy feature of the k_units data type is that it allows you the flexibility of referencing your data by row and column names and/or row and column indices (a similar feature to the MATLAB table data type, although with slightly different syntax from a MATLAB table).
Kornucopia accomplishes this by leveraging the features of MATLAB's Object Oriented Programming (OOP). Without getting too deep into the weeds of OOP, this means that the k_units data type is also a class which then allows Kornucopia to tell MATLAB how to process this data type in many MATLAB operations and functions so that your calculations work smoothly and that all the units issues, such as units conversion and units compatibility checking, are handled automatically by Kornucopia's Units Engine.
Note: if you are unfamiliar with Object Oriented Programming, do not worry! The few simple concepts of OOP that you will need to understand are explained in this help section. You do NOT need to do any Object Oriented Programming yourself to use Kornucopia. As you will see, working with the k_units data type is easy and quite natural.
Shown below is a depiction of a typical variable using the k_units data
type, followed by a depiction of the k_units
object model that shows all the "buckets" available to
hold various Properties for the variable. Also depicted at the
bottom right are the k_units
Methods, which are special functions for the data type (they
will be explained in a few moments). In the depiction, the variable matls
holds a column-oriented table-like
representation of some material information for three different materials.
The variable's Properties include
the numeric Data
(the 4 column
by 3 row array of numeric values), their associated Units
and ColNames
(column names), as
well as RowNames
and an overall
Description
Property. Additionally, if
other other Props
exist in the
variable that are not displayed to the screen, a note is provided by Kornucopia under
the output display noting this fact (in the image below, the variable
matls
does not have these extra
Props defined, so no notice appears).
Important note: As depicted in the object model above, the k_units data type is designed to hold tabular data (or scalar data). The tabular data format implies that Kornucopia data storage and calculations are column-oriented. This approach is consistent with MATLAB's table data type, but it may be different than some other traditional MATLAB work-flows that use row-wise storage and calculation set-ups.
The
k_units data type has two primary Properties, Data
and Units
, and then additional meta-info Properties
which are grouped under the Property name of Props
. Please note the following:
All the Properties accept Units can be modified after a variable of type k_units has been created. To modify the Units of an existing variable of type k_units use either the .convert or .overwriteUnits Methods.
All of the possible Properties associated with a k_units data type are described here.
Data - Holds the primary numeric data for the data type. The numeric data is always held as a double. Values placed into the Data Property that are not initially of type double are automatically converted to double via MATLAB's double function. The numeric data can be empty, a scalar, or a 2-D array.
Units - Holds the units for the Data. Units are associated to the columns of data. If units are supplied, then the Units Property will be an 1xn cell-string with the same number of elements as the number of columns in the Data property. If no units are supplied, then this property will be an empty cell array, {}.
Props - Holds all the other meta-info Properties, termed sub-Properties. All of the sub-Properties of Props will exist for any instance of the k_units data type (also known as an instance of the object).
Description - Holds an overall description of the variable, which can be empty, or one or more rows of text. Kornucopia allows you significant flexibility when specifying the Description property for Kornucopia variables such as k_units and k_func types. All three of these possibilities can be specified using MATLAB char, cell-string, or the new MATLAB string data types. Regardless of what you specify, internally Kornucopia stores the Description property as a MATLAB cell-string (a cell array of 1-row chars). For empty inputs, the Description property is stored as {}.
ColNames - Holds column names for the Data. If column names are supplied, they are held in a 1xn cell-string where n is the number of columns in Data. If no column names are supplied, the Property's value is an empty cell, {}.
ColComments - Holds column comments for the Data. If column comments are supplied, they are held in a 1xn cell-string where n is the number of columns in Data. If no column comments are supplied, the Property's value is an empty cell, {}.
ColUserData - Holds UserData particular to given columns. If column-based UserData are supplied, they are held in a 1xn cell where n is the number of columns in Data. Within each cell element you can place a numeric scalar and/or array, a struct, cell array, string, table, another k_units entity, or anything else you deem appropriate. If no column-based UserData are supplied, the Property's value is an empty cell, {}. This sub-Property was introduced in Kornucopia® ML™ version 5.2-01.
RowNames - Holds row names for the Data. If row names are supplied, they are held in a mx1 cell-string where m is the number of rows in Data. If no rows names are supplied, the Property's value is an empty cell, {}.
RowComments - Holds row comments for the Data. If row comments are supplied, they are held in a mx1 cell-string where m is the number of rows in Data. If no rows comments are supplied, the Property's value is an empty cell, {}.
RowUserData - Holds UserData particular to given rows. If row-based UserData are supplied, they are held in a mx1 cell where m is the number of rows in Data. Within each cell element you can place a numeric scalar and/or array, a struct, cell array, string, table, another k_units entity, or anything else you deem appropriate. If no row-based UserData are supplied, the Property's value is an empty cell, {}. This sub-Property was introduced in Kornucopia® ML™ version 5.2-01.
UserData - This Property can hold anything that can be held in a MATLAB variable. You can place a numeric scalar and/or array, a struct, cell array, string, table, or anything else you deem appropriate. You can also place another variable of type k_units into the UserData Property.
The basic difference between UserData and ColUserData or RowUserData is that UserData is for the entire variable where as ColUserData or RowUserData are for the specific columns or rows that they are associated to.
Excluding limits of memory, there is virtually no limit to what you put into the Property UserData (or ColUserData or RowUserData).
DimensionNames - If dimension names are specified, this Property holds a 1x2 cell-string for dimension names. If used, these are typically used as additional descriptors to describe the row and columns. If no dimension names are provided, the Property's value is an empty cell, {}.
For any variable or entity of data type k_units, you can obtain a quick overview of its Properties via the function k_summary. To actually access a variable's Properties, you use MATLAB "dot" notation, such as A.Data, A.Units, and A.Props, where A is the name of a variable. To access the sub-Properties under Props, the syntax is simply A.Props.Description or A.Props.ColNames , etc.
Some Property Limitations
The Properties of Units, Props.ColNames, and Props.RowNames have the following limitations on the char-strings that can be supplied inside their cell-strings:
None of these three Properties can contain a comma, semicolon, or colon within their strings. These characters are used as delimiters which enables Kornucopia to offer a simplified input syntax related to these three Properties that will be described later in this help page and is also described in the help section Syntax of Using k_units Data Type.
Units
limitations
Additionally, the strings supplied in Units
must be some valid combination of Units
strings that are in the Kornucopia
Units Library. If a Units
string you want to use is not in the library, you can add
it easily to the library via the k_unitsDefine
function. In addition, Units strings cannot have a dash (-) as part of their definition. To indicate
a unit that is the multiplication of two or more units, use the
"*" as in
'kg*m/s^2'. Any spaces supplied as part of a Units string
are removed by Kornucopia (ex: 'N * m'
will be converted to 'N*m').
Props.ColNames and Props.RowNames limitations
Additionally, the strings supplied in column or row names cannot
have a \* or \?
in them (they are allowed to have a * and/or ?, but not the "back-slash" version
of either). The \* and \? are reserved for future use as wildcards.
Also, any leading and trailing spaces supplied as part of
a string in a column or row name will be removed by Kornucopia,
but spaces within a name will remain. This means you can make
column or row names like the string 'true stress',
where there is a space between true
and stress.
Uniqueness requirement
for Props.ColNames and Props.RowNames
In general, the k_units
data type requires that for a given variable or entity of data
type k_units,
the column names are unique within themselves and the row names
are unique within themselves. If Kornucopia finds them to
be not unique, it will modify them as described here to be unique.
Two primary scenarios are described below. The cases describe
the handling of column names (the same approach is done
for row names).
Creating or defining a k_units data type - If duplicate column names are supplied, all the instances of the duplicate names will have "_#" appended to them where # is an appropriate sequential number.
Modifying the Props.ColNames Property of an existing entity of data type k_units - If the modification replaces only a portion of the column names, then any duplicates found within the new portion of the column names are modified with the "_#" appended to their strings. In this case, Kornucopia leaves the original names in the column names Property as they were. If, however, the modification is replacing the entire column name Property, then the behavior follows the rule stated above for creating or defining a k_units data type.
Listed below is a brief description of each of the k_units Methods. The Methods are special functions that work on k_units data types. They are typically used via a "dot" syntax call such as A.convert('mm, MPa, N') or A.toStruct, where A is the name of the k_units data type you want to apply the Method on. Arguments, if any, and specific syntax are described later on this help page and are also supplied on the specific help pages for each Method.
convert - Used to convert the Units of a k_units data type from its current Units to some other compatible Units. During a units conversion, both the Units and underlying primary Data are modified accordingly due to the change in Units. It is noted that the convert method only works on the primary Data and Units Properties and does not do any conversion of content stored in the Props.ColUserData, Props.RowUserData, or Props.UserData Properties. Click here for this Method's help page.
dataSize - Returns the size (number of rows and columns) of the data array in the Property Data. Click here for this Method's help page.
overwriteUnits - overwrites the existing Units for a variable of type k_units with the new Units you specify. No data conversions are performed. Click here for this Method's help page. Please note that this Method is typically utilized when:
you are correcting a mistake (you assigned the wrong Units to 1 or more columns of the variable),
or a variable of type k_units exists but it has no Units assigned to it and you want to assign Units to it. This generally occurs when you import in a data file that had no Units within it but you are now wanting to add the proper Units to the various columns of the variable.
toStruct - Converts an entity stored as a k_units data type into a struct data type whose field names are the names of the k_units Properties, including its sub-Properties. Click here for this Method's help page.
toTable - stored as a k_units data type into a MATLAB table data type. Click here for this Method's help page.
Note that there are a few areas where Kornucopia's k_units Properties are allowed to have more flexibility than a MATLAB table data type. In such cases, Kornucopia's internal logic will map such Properties to the MATLAB table requirements by making small modifications as needed. For full details of the mapping, see the detailed help page for the toTable Method.
To aid in determining what is a Property and what is a Method, all the k_units Properties begin with a capital letter and all of its Methods begin with a lower case letter. You can always access either using MATLAB "dot" notation, as these syntax examples below show:
B = A.Data - This would return the array of numeric data held in the Data Property of variable A.
B = A.Units - This would return the cell-string containing the Units strings for the various columns of the Data held in variable A.
B = A.Props.ColNames - This would return the cell-string containing the column names for the various columns of the Data held in variable A.
A = A.convert('mm_N_s') - This would convert all the columns in variable A from their original Units to the Units of the Units Preference named 'mm_N_s'. A Units Preference is like a Units System, but it has additional features and flexibility.
The remainder of this help page discusses and demonstrates how to use the k_units data type in MATLAB. Throughout these discussions and demonstrations, you will find a variety of hyperlinks that lead to other help pages to provide additional details on related functionality or topics.
Please note: in the demonstrations that follow, we will be typing commands in the traditional MATLAB script editor and then evaluate them, looking at their results echoed into the MATLAB Command Window. Alternatively, you can place similar commands in the MATLAB Live Script editor if you prefer that environment (although this is not demonstrated in any of the examples below).
There are four ways to create a variable (or entity) that hold a k_units data type.
Use the constructor function k_units, supplying as arguments the primary numeric data, and optionally any Units and other meta-info such as an overall description, column names, and more. You can also feed any Kornucopia-compatible variable into the constructor function k_units to create a k_units data type.
Performing any math (+, -, *, /, ^, etc) on entities where one or more of the entities involved in the math calculation are of data type k_units. The result of any such calculation will be a k_units data type.
Importing a data file into MATLAB via one of Kornucopia's functions for reading text files or importing other file formats such as Abaqus ODB or Spectral Dynamics IMPAX data files. The result of using this functionality will either be a single array of data type k_units, a cell array where each element in the cell array contains a single array of data type k_units, or a MATLAB struct or table variable type where each field (or column) holds a single array of data type k_units.
For text files, the file being read can hold more than just numeric data, it can also text for units, column names, and other sections of meta-info.
For files from other software such as Abaqus and Spectral Dynamics, Kornucopia's functionality will smartly import the data, units, column names, and other information.
Using nearly any Kornucopia function. With only a few exceptions, nearly all Kornucopia functions return a k_units data type or a cell array, MATLAB table, or structure containing k_units data types.
To further explain how the k_units data type works, we will concentrate mostly on the first two approaches from the list above. We start very simple and build our way up so that you get a good understanding of the k_units data type, and how Kornucopia and MATLAB work with it. You are encouraged to open MATLAB and type (or copy) the various commands that will be presented throughout the rest of this help section. Since we will show the result of such commands, you can also just read the help section as-is.
Please note that if you choose to type along in MATLAB, depending on the particular version of MATLAB you are using, the display formatting on your screen may look slightly different from what is presented here, but the substance should be the same.
If you are "typing along", you may choose to type commands directly into the MATLAB Command Window or type them in a MATLAB script file. If you do use the script file approach, you will of course need to use the Run Section (or Run Command) to see the results computed.
The function k_units is the class' constructor function. It has the following two calling syntax options:
A = k_units(Data, Units, ADV)
A = k_units(KornucopiaCompatibleDataType)
The argument ADV is Kornucopia terminology meaning that the function has optional Advanced arguments. In Kornucopia, each function's help page describes the ADV's for that function. Additionally, Kornucopia provides some other general features related to the use of ADV's that are described in the general help section ADV Parameter. For any Kornucopia function, you can always take default settings for any ADV options by simply omitting the ADV argument (which is what we will do in the examples that follow).
For the moment, we will concentrate on the first calling syntax approach for the k_units function (the second calling syntax is described on the k_units function's help page).
To begin, we create a variable Lx that is to hold the scalar value of 25*mm and a variable B which is to hold a 2x3 array of values with units, by column, of s, N/m^2, and in.
In MATLAB, enter and run the following commands in the traditional MATLAB script editor. Please note: In many of the calculations that are shown on this help page, we will be omitting the trailing semicolon so that the result of the calculation will automatically display in the MATLAB command window. (The special comment shown at the top of the editor, %#ok<*NOPTS> tells the editor to not issue warnings about this.)
Lx = k_units(25, {'mm'})
|
In the MATLAB environment you see the following returned:
A few notable observations:
The "Initial Kornucopia Units Preference Notice" appears the first time you use any Kornucopia-related commands, variables, or functions in a new MATLAB session. The notice tells you what is the current active Units Preference. Unless you have made some advanced Kornucopia settings, the message will show that 'SI_partial' is the activated Units Preference.
Both variables, Lx and B, display their results in the MATLAB window including their units. The variable Lx appears as a scalar while the variable B appears as a Kornucopia table.
The MATLAB Workspace states that BOTH variables are a 1x1 k_units Value of Class k_units. The "size" of 1x1 is referring to the fact that each variable is a single instance of a k_units object, it is NOT referring to the size of the primary Data property. A little bit later in this demonstration we will show a few different approaches to get at the size of the primary Data portion of variables of type k_units.
In the MATLAB commands above, we entered the units as elements of a cell array, known as a cell-string. This is because the Units Property of a k_units data type holds its contents as a cell-string. While this input syntax is proper and correct, it can quickly become annoying, especially if you have many columns of different units to define. Because the use of comma and semicolons are restricted from Units strings, Kornucopia can offer an alternative input syntax called a string-list for suppling Units. A string list is a single char-string where the units in the string are separated by commas or semicolons. Since a comma and semicolon cannot be part of the Units string itself, it can then be interpreted as a delimiter.
Thus, we can use the following simpler alternative syntax instead.
B = k_units([0, 10, 0.8; 5, 15,
1.2], 's, N/m^2, in') |
Evaluating the above in MATLAB will produce the exact same result as the more "formal" input syntax initially demonstrated. Kornucopia will internally convert the string-list into a cell-string identical to the original direct specification of the Units. You, as the user, have the freedom to use either form of input. The approach of using a string-list also works for the Props.ColNames and Props.RowNames Properties since the use of comma and semicolon are also restricted from these Properties too.
You will observe in nearly all Kornucopia examples presented throughout the Kornucopia documentation that the string-list syntax approach is typically utilized for manipulating Units, Props.ColNames, and Props.RowNames Properties as well as when referencing rows and columns in arrays.
Please note: The string-list syntax does NOT extend to the other Properties of Props.ColComments, Props.RowComments, Props.ColUserData, Props.RowUserData, Props.Description, nor Props.UserData since these Properties allow comma and semicolon use in their values (you might be making a statement or sentence in these meta-info Properties).
Depicted below are two commands using "dot" notation to define the Description Property and the ColNames Property for variable B. Following those commands is a command to extract just the contents of the Data Property from B and place it in a new variable called justNumbers. The last set of commands are an alternative all-in-one specification of the variable B using ADV options of the k_units function (click for k_units function help page to see all of its ADV options). For the creation of variable B_1 below, note that the ADV options are specified as pairs of a ParamName and ParamVal. For the ParamName 'ColNames', the ParamVal was the string-list of column names. For the ParamName 'Description', the ParamVal was a descriptive sentence (to specify multiple rows for the description, enter a cell-string).
B.Props.Description = 'A
simple data set.'; |
The output results are shown below.
Clearly, the information communicated by B is much richer than the justNumbers variable. You also see that the alternative syntax that created B_1 is indeed the same as B.
Next we show doing some simple Math and how the output results are of type k_units. Below we multiply the stress column by a value of 2. The result, C, is a k_units data type. Two other equivalent ways of doing the calculation are shown with C_1 and C_2. Lastly, we then change the ColNames Property of C and place the contents of C into the fourth column of B.
C = 2 * B('stress') |
The result from the initial calculation of C is shown below as well as the B variable after C was placed in its 4th column.
Please note the following from the above code and results:
The calculation of C (and C_1 and C_2) were derived by multiplying the stress column of B and the numeric value of 2. The order of the multiplication, 2 in front of B or after B is irrelevant. Because Kornucopia has overloaded the MATLAB math operators, MATLAB knows to pass the multiplication calculation to Kornucopia since a k_units data type was involved. The result of the math calculation is a k_units data type, the variable C. It is noted that Kornucopia has overloaded over 80 MATLAB functions and operators including many of the typical math operators such as arithmetic math operators and functions, relational operators, trigonometry, log and exponent functions, and more!
The variable C inherited some of the Properties from B during the calculation, namely the Description Property and ColNames for the stress column. See Props Inheritance to learn more about how Kornucopia handles such meta-info behaviors.
The Units for variable C changed, they are not N/m^2, as was the case for B, but rather they are now Pa for C. This is because we did math and our Units Preference was set to 'SI_partial'; remember the initial notice that was echoed at the beginning of this demonstration. In Kornucopia, whenever any math is done on a variable, the results are returned in whatever units are set as the Active Units Preference, which in this case was the 'SI_partial' system. In this units system, quantities of load/area are converted to the unit of Pa. If you want a different Units Preference active, simply use the k_unitsPreferenceActivate command in your script or calculations.
The next set of calculations compute a force from the 'double stress' and 'Vert Length' columns of B, and the variable Lx, which we defined earlier.
F2 = B('Double
stress') .* (B('Vert Length')
* Lx) |
Important items to note:
The two warnings that appear first are telling us that when we did some multiplication operations (which occurred when computing the variable F2) that none of the Props were inherited. This means that none of the Props Properties, of B('Vert Length'), Lx, nor B('Double stess') are being inherited by F2. As a result, F2 has not inherited any column names from these quantities and thus F2 has an empty ColNames Property. To be clear, all the numeric values and Units from the Data and Units Properties are properly processed, it is only the meta-info Properties in Props that are not carried through the calculation. This behavior, in total, is reasonable because there is no consistent logic to apply to derive a column name for F2 based on the entities used to create it. Bottom line, in this case, everything is fine and the warnings can be ignored. See the help page on Props Inheritance to learn more, including how to suppress this particular type of warning.
Because F2 does not have a column name, when we append it to the end of B (which has column names), the new result of B automatically fills-in the last column with a default column name. Once a variable (or entity) of data type k_units has ColNames, then any additional columns added to it must have column names. If they do not, Kornucopia assigns a default one for you. In the final version of B displayed above, we have changed the ColNames content of only the last column to be a more reasonable name, 'Force'.
As for the actual calculation of the force variable, F2, Kornucopia automatically handled all the Units-related conversions and simplifications needed. Note that the 'Vert Length' column was in units of in where as Lx was in units of mm and 'Double stress' was in Pa. The final result was in N because the Active Units Preference was 'SI_partial'.
The calculations below show a few of the ways you can easily convert the units of a whole variable, or specific columns, to other specific units or a different Units Preference.
% Convert
all columns using Active Units Preference |
The result from the four different uses of the convert Method are shown below.
As you can see, the syntax and capabilities are very flexible. You can learn more about converting units on the convert Method's help page. If you are curious about the displayed precision of Kornucopia variables, click here to learn more.
We now switch to a slightly more elaborate demonstration. The calculations below create a decaying velocity vs. time curve using some analytical equations and then plot the result.
k_cleanup() |
Several things are occurring in the commands presented (click on the hyperlinked entities to learn more as needed):
First, the function k_cleanup is used to provide interactive pop-up offering to clean-up the workspace.
The Units Preference 'mm_N_ms' is activated via k_unitsPreferenceActivate.
A global Kornucopia setting is modified to turn off Props Inheritance warnings. Click these links to learn more about the k_set function and Props Inheritance.
The function k_unitsVariables is utilized to create the variables s, msec, Hz, and m in the MATLAB workspace.
Using this function is equivalent to issuing the following commands, but more compact and easier to do.
k_units(1, 's'); k_units(1, 'msec'); k_units(1, 'Hz'); k_units(1, 'm');
The variables representing units are then used in calculations to compute several items. Since these Units Variables are of data type k_units, all the math operations using them automatically creates other variables of data type k_units.
To calculate the decaying sinusoid, the k_window function was used (an actual equation for an exponential decay could also be used, but this was easier). It is noted that the element-by-element multiplication syntax of ".*" was needed since the result of the sin function (due to variable t) and k_window function are both column vectors and we do NOT want to compute a matrix dot product.
The k_plot function not only plotted the data, but it leveraged our earlier efforts of adding a few descriptive properties to also automatically label and title the plot!
Depicted first is the clean-up pop-up and a view of the variables in the workspace after the commands were run.
The image below shows the output echoed to the MATLAB command window and the figure that was plotted.
Important items to note:
Because of the Activated Units Preference, computed variables are converted to the units associated with that preference. The image shown below this section of notes depicts many of the Units Preferences available in Kornucopia and the specific one of mm_N_ms that was activated. In brief, any calculation results are converted to the units listed in the specified preference. Click here to learn more.
No Props inheritance warnings appear above because we turned them off. Had we not, a warning would have been generated by the multiplication operations related to calculating the variable v.
The display of variables t and v did NOT go scrolling off the screen, but instead displayed a few lines and then ended with a note of how many rows are not shown along with a tip of how to see more rows. This unique Kornucopia behavior makes it very easy to take a quick view at variables and their contents in the MATLAB window without them scrolling off the screen.
You can also view the variables in the MATLAB variable editor or via the function k_varViewer, which opens a nice scrollable variable viewer. Click here for a summary of viewing options.
With little effort on the user's part, the plot is created that includes automatically created, well-labeled axes and a title.
Displayed below is the result of using the k_unitsPreferenceList function at the MATLAB command prompt.
The calculations below differentiate and integrate the velocity vs time curve created previously to obtain curves of acceleration and displacement. All the important variables are then concatenated into a single array which is then plotted.
adv = k_adv(); |
Some important items to note about the calculations above are:
A special variable, adv, was defined using the k_adv function. This function returns a structure of all the Kornucopia functions that have ADV options and the various options for each function. This adv variable is then easily used to obtain a scrollable selection list of ADV options for any function (which is done in the last k_plot use at the end of the set of commands shown below). Users find this adv variable technique extremely handy.
Just like math (via Kornucopia's k_units data type), the Kornucopia functions of k_derivative and k_integrate know how to handle all the units-related conversions involved in the calculations.
Grouping all the variables of t, d, v, and a into a single array is natural and easy by using normal MATLAB concatenation syntax.
Since the fundamental Units Signatures of displacement, velocity, and acceleration are different, we must use three different plots to graphically show all the data in the same figure window. The MATLAB function subplot helps easily divide the Figure window into three axes (regions) for the plots.
When we create the figure window, we use the 'position' option of the MATLAB function figure and supply the Kornucopia function k_figZoom as the value argument. This allows us to easy change the figure window size without having to specify pixel locations on the screen. The arguments of k_figZoom are the zoom factors for the width and height.
The three different instances of k_plot used below are showing a variety of syntax options, including Flexible XY Data Input Syntax to plot all the variables. Plotting with Kornucopia is very flexible. You can learn more here.
In the last plot of B (the acceleration vs time curve), we utilized a handy option of converting units just for the plot.
Presented below is the combined result variable B and the final plot of the results.
To close-out the demonstration, we show a how to obtain the size of the primary data array within a k_units data type and also how to work with MATLAB functions that do not support the k_units data type. The snippet of commands below and resulting output presented after the commands shows four different ways to obtain size information.
size(B) |
Items of note regarding the four approaches and their results:
The MATLAB function size returned the size of 1,1 (meaning 1 row and 1 column). This result occurs because the size function looks at the k_units data type as a whole entity, it does not know to look at just the Data Property to determine size. It is noted that while the size function could have been overloaded by Kornucopia to return the size of the Data Property, such an overload is NOT implemented because it would have very undesirable results on the MATLAB variable editor behavior with k_units data types. This is why variables of data type k_units appear as 1x1 k_units in the MATLAB Workspace window.
The second command leveraged dot notation to directly access the Data Property of B. This returns the numeric array from the primary data container of B on which the function size then computes the desired result.
This approach of directly accessing the Data Property is an easy way to get just the numeric content from a k_units data type variable to supply to MATLAB functions that do not yet have an overload definition. This means, if you try to use a MATLAB function and it complains it does not know what a k_units data type is, simply feed the function the variable as B.Data,(where B is your variable name); you will then be supplying the MATLAB function just numbers. Other approaches to convert ALL of the k_units data type to a more traditional MATLAB data type include the Methods toTable and toStruct.
The third command shows how you can also use the Method dataSize to obtain the array size of the Primary data container of B.
The last command uses the k_summary function to obtain a concise summary of the variable and its Properties.
For many commonly used MATLAB functions, Kornucopia has overloaded these MATLAB functions so that they natively understand how to take input arguments of type k_units. Having said this, MATLAB has thousands of functions, so there quite a few MATLAB functions that are not overloaded by Kornucopia, and thus those functions will not directly take input arguments of type k_units. This is easily to get around by sending in only the numeric data portion of the input arguments using the .Data property of any variable of type k_units. Below is a simple example demonstrating the technique.
Consider the case where you have a vector of velocity values that are represented via a k_units data type. Below is the echo of the variable v (created previously in this example) in the MATLAB command window.
We desire to compute a moving median of this data with a window length of 10 data points. MATLAB's movmedian function does just what we want, so we try the following but MATLAB throws an error:
MATLAB is complaining that it does not understand how to process the 1st argument, v, which is of data type k_units. To solve this issue, we need to send in only numeric data that represents the variable v, essentially removing units (or stripping units) from the variable. This is easily done by using the .Data property of the variable which holds only the numeric portion of the variable. Notice the following:
Thus we can easily compute the moving median as:
The result depicted above holds the desired calculation, BUT it is now just numbers. There are no Units, colNames, etc. Below is a better implementation where we still use the .Data approach, but also put the result back into a k_units data type.
In the few extra lines used above, the 2nd line takes the numeric result tmp and puts it into a k_units data type using the .Units from v. The last 2 lines add an appropriate column name to the output variable b and a description too.
To learn more about the k_units data type and using Kornucopia in general, consider the following links.
More details on k_units
Syntax of Using k_units Data Type - Highly recommended you review this help page next.
Details on Kornucopia Features
General Kornucopia usage