Utilities

applyToNonNan(data, func)[source]

Apply the given function columnwise to all non NaN values in the given data.

USAGE:

result = applyToNonNan (data, func)

INPUTS:
  • data – Matrix of data (individuals x variables)

  • func – function handle that takes an vector of data and computes single value result.

OUTPUTS:

result – A Vector of with dimensions (size(data,2) x 1) where the supplied function was applied to all columns ignoring NaN values.

calcDist(searchString, databaseString)[source]

Calculate a Distance between the searchString and the database string. a perfect match will return a distance of 0. The distance is a modified levenshtein edit distance. leading or trailing elements in the search string have a cost of 0.8 leading or trailing elements in the database String have a cost of 0.1 Uppercase <-> lower case edits have a cost of 0.05 All other edit operations have a cost of 1

USAGE:

d = calcDist (searchString, databaseString)

INPUTS:
  • searchString – The string that is used as a query

  • databaseString – The string in a database that the search string is compared to.

OUTPUT:

d – A Distance between the searchString and the databaseString.

Note

This function is not a metric i.e. calcDist(a,b) ~= calcDist(b,a) !

clearGlobal(globalName)[source]

Safely clear a global variable.

USAGE:

clearGlobal (globalName)

INPUTS:

globalName – The name of the global variable to clear.

columnVector(vec)[source]

Converts a vector to a column vector

USAGE:

vecT = columnVector (vec)

INPUT:

vec – a vector

OUTPUT:

vecT – a column vector

countUnique(list)[source]

Count unique elements in a vector (cell array or numerical) Also sorts the unique elements in descending order

USAGE:

[sortedList, sortedCount] = countUnique (list)

INPUT:

list – input vector

OUTPUTS:
  • sortedList – list with sorted elements

  • sortedCount – number of elements

dispMatrix(A, mode)[source]

display a matrix A in a tight format

USAGE:

dispMatrix (A, mode)

INPUTS:

A – m x n matrix

OPTIONAL INPUTS:

mode – format used to display A (char, default = ‘Z’):

  • ‘disp’ - display A using disp

  • ‘N’ - one space-separated natural number per entry

  • ‘Z’ - integers, right-aligned to a common width

  • ‘nonzeroZ’ - same as ‘Z’, after removing all-zero rows/columns

  • ‘nonzeroN’ - same as ‘N’, after removing all-zero rows/columns

duplicates(A)[source]

create a map M between the first instance (row) and other instances (cols) of an equivalent set using [C,IA,IC]=unique(A,’rows’,’stable’);

USAGE:

[M, duplicateBool, C, IA, IC] = duplicates (A)

INPUTS:

A – m x n array (compatible with unique)

OUTPUTS:
  • M – m x m array where M(i, j) = 1 if the first instance of i has a duplicate j, and M(i, j) = 0 otherwise.

  • duplicateBool – m x 1 boolean, true for each row of A that is a duplicate of an earlier row.

  • C – unique first instances in the same order that they appear in A

  • IAC = A(IA, :)

  • ICA = C(IC, :)

Example

A = [‘a’;’a’;’b’;’c’;’d’;’d’;’b’;’b’;’e’]; % M = % 0 1 0 0 0 0 0 0 0 % 0 0 0 0 0 0 0 0 0 % 0 0 0 0 0 0 1 1 0 % 0 0 0 0 0 0 0 0 0 % 0 0 0 0 0 1 0 0 0 % 0 0 0 0 0 0 0 0 0 % 0 0 0 0 0 0 0 0 0 % 0 0 0 0 0 0 0 0 0 % 0 0 0 0 0 0 0 0 0 % duplicateBool = % 0 % 1 % 0 % 0 % 0 % 1 % 1 % 1 % 0

extendIndicesInDimenion(input, dimension, value, sizeIncrease)[source]

Extend a specified dimension of the given array by appending values

USAGE:

added = extendIndicesInDimenion (input, dimension, value, sizeIncrease)

INPUTS:
  • input – The input matrix or array

  • dimension – The dimension in which to add values for the given indices

  • value – The value to append in the given dimension.

  • sizeIncrease – How many entries to add.

OUTPUT:

added – The Array/Matrix with the given indices set to the default values.

generateStructFunction(S, structName)[source]

Generate a .m script that reconstructs the numeric/char-valued fields of a struct

Writes a file named <structName>_init.m containing one assignment statement per field of S that is numeric or char, so that running the generated file recreates those field values under the variable name structName.

USAGE:

generateStructFunction (S, structName)

INPUTS:
  • S – struct whose numeric and char fields are written out

  • structName – char, the variable name used in the generated assignment statements and the base name of the generated <structName>_init.m file

Note

Fields of S that are neither numeric nor char (e.g. cell, struct) are silently skipped.

getDefaultTableRow(tableRow)[source]

get a default table row :USAGE: defaultRow = getDefaultTableRow (tablerow)

INPUT:

tableRow – The row for which to define a default row

OUTPUT:

defaultRow – A row with default values for different data types

getDefaultValue(value)[source]

get the default value for a given value (NaN for numeric, empty strings for textual, false for logical) :USAGE: defValue = getDefaultValue (value)

INPUT:

value – The value to get a default for

OUTPUT:

defValue – The default value for the given values type.

getEnvironment()[source]

Get all values of current globals in a struct.

USAGE:

environment = getEnvironment()

OUTPUT:

environment – a struct with two fields

  • .globals - contains all global values

  • .path - contains the current path

getGlobalValue(globalName)[source]

Safely get the Value of a global variable.

USAGE:

value = getGlobalValue (globalName)

INPUTS:

globalName – The name of the global variable to get the value for

OUTPUT:

value – The value of the requested global variable

issueConfirmationWarning(message)[source]

Issues a warning, which and blocks execution until confirmation by the user.

USAGE:

issueConfirmationWarning (message)

INPUTS:

message – The message to be displayed

isvar(T, varName)[source]

Check if varName is a field of struct T, or a column (variable) of table T

USAGE:

[tf, bool, ind] = isvar (T, varName)

INPUTS:
  • T – struct, or table to search. When T is a table, fields used are:

    • .Properties - table metadata; Properties.VariableNames is read to test membership of varName

  • varName – char, the field/variable name to look for

OUTPUTS:
  • tf – true if varName is present in T

  • bool – logical (vector for a table, scalar for a struct) indicating which entries of T match varName

  • ind – index of the match (1 for a struct; the column index/indices for a table)

mapAontoB(Akey, Bkey, Ain, Bin, printLevel)[source]

Maps the data from Ain onto Bin by matching keys from Akey onto Bkey If Akey is not unique, it maps the lowest absolute index in Akey to the corresponding Bkey, but gives a warning, unless printLevel=0;

USAGE:

[Bout, LIBkey, LOCAkey] = mapAontoB (Akey, Bkey, Ain, Bin, printLevel)

INPUTS:
  • Akey – m x 1 primary key in array or table Ain

  • Bkey – n x 1 primary key in array or table Bin

  • Ain – m x z array or table. When Ain is a table, fields used are:

    • .Properties - table metadata; Properties.VariableNames is used to build Bout, and Properties.Description is copied to the output table

OPTIONAL INPUTS:
  • Bin – n x y array or table, which is created starting from Bkey if Bin is not provided.

  • printLevel – verbosity of the non-unique-Akey warning (default = 1; 0 suppresses the warning)

OUTPUTS:
  • Bout – n x y array or table, which is created starting from Bkey if Bin is not provided.

  • LIBkey – n x 1 array of the same size as Bkey containing true where the elements of Bkey are in Akey and false otherwise. Output from [LIBkey, LOCAkey] = ismember(Bkey, Akey).

  • LOCAkey – n x 1 array containing the lowest absolute index in Akey for each element in Bkey which is a member of Akey, and 0 if there is no such index. Output from [LIBkey, LOCAkey] = ismember(Bkey, Akey).

removeIndicesInDimenion(input, dimension, indices)[source]

Remove the indices in a specified field in the given dimension

USAGE:

removed = removeIndicesInDimenion (input, dimension, indices)

INPUTS:
  • input – The input matrix or array

  • dimension – The dimension from which to remove the indices

  • indices – The indices to remove

OUTPUT:

removed – The array/matrix with the given indices removed.

removeZeroRowsCols(A)[source]

Removes all zero rows and columns from a matrix

USAGE:

A = removeZeroRowsCols (A)

INPUTS:

A – m x n matrix

OUTPUT:

AA with all zero rows removed, followed by all zero columns removed

restoreEnvironment(environment, restorePath, printLevel)[source]

Reset all global variables to a value stored in the input struct (all variables not present will be deleted. :USAGE: restoreEnvironment (globals)

INPUTS:
  • environment – A struct with the following fields: * .globals: a struct with the fields being global variables and the value the respective values. * .path: the path to restore (it will override the current path)

  • restorePath – Also restore the path (default: true)

  • printLevel – Set the verbosity of this method: * 0: No outputs (Default) * 1: Info what each value is set to

setGlobal(globalName, globalValue)[source]

Safely set a global Variable to a specific value.

USAGE:

setGlobal (globalName, globalValue)

INPUTS:
  • globalName – A string representing the name of the global variable

  • globalValue – The value to set the global variable to

showprogress(x, whichbar)[source]

showprogress shows waitbars

USAGE:

fout = showprogress (x, whichbar)

INPUTS:

x – percentage in integer (e.g.: 1 = 1%, 40 = 40%, etc.)

OPTIONAL INPUTS:

whichbar – caption passed to the underlying waitbar/textprogressbar

OUTPUT:

fout – handle output from waitbar() (WAITBAR_TYPE = 1)

unioncell(A, colA, B, colB)[source]

Return a cell which is the union of cell B to cell A given by a comparing

USAGE:

AB = unioncell (A, colA, B, colB)

INPUTS:
  • A – cell array A

  • colA – column of A for comparison

  • B – cell array B

  • colB – column of B for comparison

OUTPUT:

AB – cell which is the union of cell B to cell A

updateStructData(origStruct, updateStruct)[source]

Update the struct in origStruct with the data from updateStruct

USAGE:

updatedStruct = updateStructData (origStruct, updateStruct)

INPUTS:
  • origStruct – The original Struct

  • updateStruct – The struct to update the information in origStruct.

OUTPUT:

updatedStruct – The struct with the information from origStruct updated by the info from updateStruct