A collection of free and open source Matlab tools.
Andreas Sommer, 2010-2026
E-Mail: code@andreas-sommer.eu
--> Open this README on Github <--
Tools marked with ⭐ might be especially worth a look.
- ADLER32 - Compute Adler32 hash [->code]
- condSet - Conditionally set values in (cell) array/matrix/vector [->code]
- DEBUGME - Debug marker/helper for changed values [->code]
- execWSL - Execute command in WSL (Windows Subsystem for Linux) [->code]
- filecopy - Copy individual files [->code]
- findfield - Find field in struct by regular expressions [->code]
- findFirstGreater - Finds first array entry greater than given value [->code]
- findFirstGreaterRev - Finds first array entry greater than given value (from end) [->code]
- findFirstSmaller - Finds first array entry smaller than given value [->code]
- findFirstSmallerRev - Finds first array entry smaller than given value (from end) [->code]
- findNearestNeighbor - So much more than just finding the nearest neighbor [->code]
- getCaller - Retrieve calling function, file, line number [->code]
- getParentFigure - Retrieve figure containing given handle [->code]
- getWorkspaceVariable - Retrieve variable from other workspace [->code]
- hornereval - Evaluate 1d polynomial using Horner's scheme [->code]
- hornereval2D - Evaluate 2d polynomial using Horner's scheme [->code]
- integrate_with_restarts⭐ - Integrate implicitly switched ODE with state jumps [->code]
- isfigure - Check if variable is handle to a figure [->code]
- istext - Check if variable is a char array or a string [->code]
- makeClosure - Mimick pass-by-reference via closure [->code]
- makeMessage - Message generating with preponed Caller [->code]
- makeNestedStruct - Generate nested struct from tokenizable string [->code]
- msession⭐ - Store and retrieve Matlab sessions (open files, variables, etc.) [->code]
- optionlists⭐ - Handle name-value pairs [->code]
- pointpicker - Pick and collect coordinates by clicking in figure [->code]
- roundto - Rounds values to nearest divisor value [->code]
- stopOnKeyPress - displays a stop figure that reacts on key press [->code]
- sviz - Simple visualizer [->code]
- truncate_arrays_in_struct - Truncate long array fields in a struct [->code]
- whichToolboxFor - Investigate toolbox dependency of code [->code]
Documentation is provided inside the code and thus available using Matlab's help system via help and doc.
ADLER32 [see code]
Computes the Adler32 hash of a given char array.
condSet [see code]
Conditionally sets values in (cell) array/matrix/vector.
Can also evaluate function on the elements of a given (cell) array/matrix/vector at places where
the specified condition is true or false.
Can also be used as ternary operator if the last argument is an empty matrix (see last example)
% EXAMPLE CALLS:
M = magic(5); R = -rand(5); % matrices for testing
z = condSet(M>10, 1, 5) % --> matrix of size(M) with 1 where M>10, and 5 otherwise
z = condSet(M>10, 1, 5, {}) % --> cell array of size(M) with 1 where M>10, and 5 otherwise
z = condSet(M>10, '2BIG') % --> cell array of size(M) with '2BIG' where M>10
z = condSet(M>10, 10, M) % --> copy of M with value 10 where M>10
z = condSet(M>10, @(x) x^2, -1, M) % --> copy of M with squared entries where M>10, and -1 otherwise
z = condSet(M>10, R, {}, M) % --> copy of M with values from R where M>10
z = condSet(tf, 'YES', @sin, []) % --> returns 'YES' if tf is true, otherwise @sinDEBUGME [see code]
Debug helper for changed valued. It transparently returns the new value while displaying as message about that.
The message can be freely configurated. Helpful to not forget to undo test changes.
var = sin(x + 0.5); -- original code to be modified
var = sin(x + DEBUGME(1.5)); -- delivers value 1.5 and displays standard debug marker message
var = sin(x + DEBUGME(1.5, 'SIN offset changed to %g')); -- delivers 1.5 and displays individual message
Special command initiated with # allow configuration of debug output.
- The printer for the debug message can be chosen via special command
#printerand set to any function handle that can parse fprintf like input, e.g.@fprintfor@warning - Quick reset can be done using special command
#reset - See code for details.
execWSL [see code]
Executes a command in WSL (Windows Subsystem for Linux).
Distribution can be chosen. Dryrun and echoing supported.
filecopy [see code]
Copy individual files in an operating system independent way. In contrast to Matlab's copyfile(), this filecopy() does not transfer permissions from the source but creates the destination file with the current user's permission set.
findfield [see code]
In a struct with many (several hundreds) of fields, finding the correct field name can be cumbersome. The findfield() function allows to search for fieldnames by string patters or regular expressions, and also provides information if an exact match is found.
findFirstGreater [see code] | findFirstGreaterRev [see code] | findFirstSmaller [see code] findFirstSmallerRev [see code]
Firns first entry in array that is greater or smaller than a specified value, searching from beginning or from the end (reversed).
Optionally starts search at given index.
findNearestNeighbor [see code]
Searches an array x for nearest neighbors of query values xq and returns indices and values (vectorized!).
The user can choose the type of "neighbor": the closest, the next smallest, or the next greatest value.
If a query value is outside the range of the values in x, either the closest value in the array or an error marker can be returned upon user choice.
Pre-sorted search arrays are used for performance. On unsorted arrays x and/or xq, when querying multiple values xq, the arrays x and/or xq are sorted
first and the sorted arrays may be delivered upon request. When querying a single value xq in an unsorted array x, no presort is done.
hornereval [see code]
Evaluation of 1d polynomials using Horner's scheme.
hornereval2D [see code]
Evaluation of 2d polynomials using Horner's scheme.
getCaller [see code]
Retrieve calling function, optionally with file name and line number. Relies on Matlab's dbstack.
getParentFigure [see code]
Retrieve the handle of the figure that contains the specified graphics handle.
getWorkspaceVariable [see code]
Retrieve a variable from other workspace (base or caller), with optional not-found value and error signaling capability.
integrate_with_restarts⭐ [see code]
A Matlab tool for integration of switched ODEs, with implicit (state-dependent) model and state changes.
Only integration is supported.
The tool IFDIFF is much more sophisticated.
It generates switching functions automatically from existing code with IF statements and can also compute forward sensitivities.
isfigure [see code]
Queries if specified handle refers to a (valid) figure.
istext [see code]
Checks if specified object is a char array or a string
makeClosure [see code]
Generates a closure to mimick pass-by-reference style of programming.
makeMessage [see code]
Display or generate message and prepone the calling function. makeMessage is a wrapper around Matlab's *printf functions, but accepts also other printer functions that follow the sprintf or fprintf API.
makeNestedStruct [see code]
Generates/extends a nested struct by tokenizable strings.
Example:
names = { "aaa::bbb::ccc", "aaa::bbb:ddd", "aaa::ddd", "eee" };
values = { 42, 'hi there' , 'hello' , pi };
s = makeNestedStruct([], names, values, '::');This will create a structure with the following layout:
s.aaa.bbb.ccc = 42
s.aaa.bbb.ddd = 'hi there'
s.aaa.ddd = 'hello'
s.eee = 3.141569
msession⭐ [see code]
Stores a whole Matlab work session in a file, and restores it upon request. The user can select what to be stored:
- open files
- main work space variables
- global variables
optionlists⭐ [see code]
Matlab tools for handling name-value pairs, especially in function calls.
- querying arguments by name:
olGetOption - checking for present arguments:
olHasOption - generation of option lists:
olSetOption - removing from option lists:
olRemoveOption - rename existing options:
olRenameOption - checking validity:
olIsOptionlist - checking validity with assertion:
olAssertOptionlist - retrieving list of all names:
olCollectOptionNames - retrieving list of all values:
olCollectOptionValues - warn upon unprocessed arguments:
olWarnIfNotEmpty
For details, see, help olGetOption, etc.
Call:
val = f(a,b,'name','test','age',35,'numbers',{1,7,2})Function code:
function val = f(a,b,varargin)
% a and b are normal position-dependent arguments.
% Further arguments are (usually) optional and initialized by default values.
% Set default values
name = 'defaultname';
age = 0;
numbers = {1,2,3,4,5};
% Query optional arguments:
if olHasOption(varargin, 'age' ), age = olGetOption(varargin, 'age' ); end
if olHasOption(varargin, 'numbers'), numbers = olGetOption(varargin, 'numbers'); end
% Alternatively, default arguments can be specified in olGetOption directly if key is not found:
name = olGetOption(varargin, 'name', '[UNKNOWN-PERSON]');
% Program code
% ...
end The syntax [value, remainingOptions] = olGetOption(options, key) can be used to remove key
from the optionlist. This is useful to check if there are some options left after processing
using olWarnIfNotEmpty.
pointpicker [see code]
This tool is intended to collect point coordinated from a matlab figure, and pick and collect points by clicking.
Register to the current axis using pointpicker(gca()).
The pointpicker may be registered to multiple figures/axes, but only collects points from the currently active one.
By pressing key 'a' while in the registered axis, the mouse changes to a crosshairs and multiple points can be collected;
the last one picked can be deleted by pressing 'd'. Collection is ended by pressing 'x'.
Collected points may be retrieved into a variable by points = pointpicker('#GET') or saved to file.
roundto [see code]
Rounds values to the nearest divisor value. See roundto_example.m for an example.
stopOnKeyPress [see code]
Opens a figure that listens to key pressed and checks if a stop key is hit. The figure can also display a message, some progress information, or an updating counter. See the code for a detailed usage with example.
sviz [see code]
Simple visualization tool.
truncate_arrays_in_struct [see code]
Truncates all arrays (also cell arrays) that are 1st level fields in a struct to specific indices. Scalar fields, chars or strings are left unmodified. Also arrays that do not have sufficient size are unmodified.
whichToolboxFor [see code]
Retrieves the required Matlab Toolboxes for specified mfile. Also inspects all files invoked by mfile and checks their dependency. Additionaly prints for every used toolbox the list of files that actually require them.