A collection of free and open source Matlab tools.
Andreas Sommer, 2010-2024
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]
- findfield - Find field in struct by regular expressions [->code]
- findFirstGreater - Finds first array entry greater than given value [->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⭐ - Integrade 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]
- msession⭐ - Store and retrieve Matlab sessions (open files, work space 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]
- sviz - Simple visualizer [->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.
% 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
DEBUGME [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
#printer
and set to any function handle that can parse fprintf like input, e.g.@fprintf
or@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.
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]
Firns first entry in array that is greater than a specified value. Optionally starts search at given index.
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.
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.
sviz [see code]
Simple visualization tool.
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.