Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aminya/dispatch.m
Runtime multiple dispatch for Matlab.
https://github.com/aminya/dispatch.m
argument dispatch matlab multiple-dispatch varargin varargs
Last synced: 10 days ago
JSON representation
Runtime multiple dispatch for Matlab.
- Host: GitHub
- URL: https://github.com/aminya/dispatch.m
- Owner: aminya
- License: other
- Created: 2020-01-22T05:40:32.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-09-07T05:06:33.000Z (over 2 years ago)
- Last Synced: 2024-12-06T13:32:21.531Z (22 days ago)
- Topics: argument, dispatch, matlab, multiple-dispatch, varargin, varargs
- Language: MATLAB
- Homepage:
- Size: 18.6 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Dispatch.m
Runtime multiple dispatch for Matlab.- Dispatch based on the number of arguments
- Dispatch based on the type of arguments
- Supported for code generationWrite a function like the following example as a template. Use `dispatch(varargin, methodTable)` function to invoke methods.
```matlab
function varargout = foo(varargin)methodTable = {@foo1, ["any"]; % dispatch based on number of inputs
@foo2, ["logical","logical"]; % dispatch based on type
@foo3, ["numeric", "logical"];
@foo3, ["logical", "numeric"]; % repeated method for different type
@foo4, ["Person"]; % dispatch on class
@foo5, ["any", "logical"]};[varargout{1:nargout}] = dispatch(varargin, methodTable);
end
```Wrtie different functions as methods.
```matlab
function out = foo1(a)
out = a;
endfunction out = foo2(a, b)
out = logical(a && b);
endfunction out = foo3(a, b)
out = a * b;
endfunction [out1,out2] = foo4(p)
out1 = p.name;
out2 = p.age;
endfunction [out1,out2] = foo5(a,b)
out1 = a;
out2 = b;
end
```Now let's test the example:
```matlab
% dispatch based on number of inputs
>> foo(2)
ans =
2
```
```matlab
% dispatch based on type
>> foo(true, false)
ans =
logical
0
```
```matlab
% dispatch based on type
>> foo(2, true)
ans =
2
```
```matlab
% dispatch on number of output args
>> p = Person("Amin",25);
>> foo(p) % dispatches on foo1
ans =
Person with properties:
name: "Amin"
age: 25>> [a,b] = foo(p) % dispatches on foo4
a =
"Amin"
b =
25
```
```matlab
% dispatch on any type
>> foo({2},true)
ans =
logical
1
```
```matlab
% error handling
>> foo({2},p)
error: no method found
```# Note
- ~~You can't have multiple outputs for your function. Instead return the outputs as an array or cell of outputs.~~ FIXED by @bellomia, with a soft change in API: the top-level wrapper (`foo` in the example) has to feature the [`varargout`](https://www.mathworks.com/help/matlab/ref/varargout.html) syntax.- You can't dispatch on the name of the structs. Instead define simple class with just properties (See Person).
# License
This is written as part of my Master's thesis and it is licensed under Apache V2, so cite this paper if you use it:
```
A. Yahyaabadi, P. Ferguson, ”An intelligent multi-vehicle drone testbed for space systems and remote sensing verification,” in Canadian Aeronautics and Space Institute (CASI) ASTRO, Canada, 2019
```
In case of changes, either make pull requests to this repository or state the changes.