CommandChainer

Version 2.0 (2.96 KB) by TADA
Tool for chaining commands on objects or structures in a single line. Handy for running several operations in an anonymous function
22 Downloads
Updated 25 Nov 2018

View License

CommandChainer Wraps objects (struct/class) to allow chaining of commands
This little tool is handy for chaining several commands in a single line of code (who cares? think of anonymous functions...)

use the ctor to generate a CommandChainer object wrapper:
cc = CommandChainer(object/struct)

use the set function to set field or property and get cc back for further commands:
cc.set('prop/field name', value) or set(cc, 'prop/field name', value)

use the execute function to execute a function in the wrapped object and get cc back for further commands:
cc.execute('function name', arg1, arg2, arg3, ...) or execute(cc, 'function name', arg1, arg2, arg3, ...)
* function args are optional

use the invoke function to invoke a function in the wrapped object and get the returned arguments:
[out1, out2, out3, ...] = cc.invoke('function name', arg1, arg2, arg3, ...)
or
[out1, out2, out3, ...] = invoke(cc, 'function name', arg1, arg2, arg3, ...)
again, in/out args are optional, but if you don't have out args, this method is not relevant really...

use the get function to return the value of a specific property:
out = cc.get('prop/field name') or out = get(cc, 'prop/field name')

use the execute function combined with the result/resultCell propeties to use in an anonymous function
result property returns the first out argument of the last executed function
resultCell property returns a cell array containing all the output arguments of the last executed function
* Apparently method chaining is not documented in Matlab, and may break in future releases
* In R2017b it is possible to declare an anonymous function with chained commands as long as the chain ends with a property and not a function
out1 = CommandChainer(obj).set('prop1', val1).set(, 'prop2', val2).execute('foo1', arg1, arg2, arg3).result
[out1, out2, out3, ...] = CommandChainer(obj).set('prop1', val1).set(, 'prop2', val2).execute('foo1', arg1, arg2, arg3).resultCell

now we can do this:
[out1, out2, out3, ...] = CommandChainer(obj).set('prop1', val1).set(, 'prop2', val2).execute('foo1', arg1, arg2, arg3).invoke('foo2', arg1, arg2, arg3)
or
[out1, out2, out3, ...] = invoke(execute(set(set(CommandChainer(obj), 'prop1', val1), 'prop2', val2), 'foo1', arg1, arg2, arg3), 'foo2', arg1, arg2, arg3)

Using function notation instead of dot indexing is documented and can be used in an anonymous function like that:
foo = @(x) invoke(execute(set(execute(set(set(CommandChainer(x), 'prop1', value1), 'prop2', value2), 'foo', arg1, arg2, arg3), 'prop3', value3), 'foo2', arg), 'doSomething');
[out1, out2, out3, ...] = foo(obj);

Cite As

TADA (2024). CommandChainer (https://www.mathworks.com/matlabcentral/fileexchange/69276-commandchainer), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2017b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Programming in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
2.0

Added properties for accessing result of last executed function for chaining in an anonymous function

1.0.2

get method had a copy paste error

1.0.1

fixed summary

1.0.0