Creating a custom function in C++ visible to Matlab through Image acquisition toolbox

3 views (last 30 days)
Hi
I am writing an adaptor with image acquisition toolbox and I have a function written in C++ which sends a command to hardware and I want to call this function on Matlab.
I understand the image acquisition toolbox has predefined functions such as openDevice(), startCapture() and etc but is it possible to create a custom function that is visible to Matlab ? I also understand that we can call functions in C++ with loadlibrary and calllib from a DLL, but this must be outside of image acquisition toolkit and I want this custom function to be called through image acquisition took kit.
If anybody knows, please let me know :)

Answers (1)

sanidhyak
sanidhyak on 2 Sep 2025
I understand that you are trying to create a custom function in C++ that communicates with your hardware and make it visible in MATLAB through the Image Acquisition Toolbox adaptor. By default, the toolbox provides predefined methods such as “openDevice()”, “startCapture()”, etc., but it is also possible to expose more custom functions through the adaptor so that they can be called directly from MATLAB.
To do so, you need to extend your adaptor implementation and register your custom method. Kindly refer to the following approach:
// Example custom method inside your adaptor
void customCommand(const std::string& cmd) {
// Hardware-specific logic here
mexPrintf("Custom command executed: %s\n", cmd.c_str());
}
Next, you need to register the method so that MATLAB recognizes it:
void MyAdaptor::getAdaptorInfo(imaqkit::IAdaptorInfo* info) {
info->setAdaptorName("MyAdaptor");
// Register custom method
info->addDeviceSpecificMethod("customCommand", "char");
}
Finally, implement the dispatcher to handle MATLAB calls:
bool MyAdaptor::deviceSpecificMethod(const char* methodName,
const imaqkit::IPropInfo*,
const imaqkit::IMethodArgs* args) {
if (strcmp(methodName, "customCommand") == 0) {
const char* cmd = args->getString(0);
customCommand(std::string(cmd));
return true;
}
return false; // Unknown method
}
After compiling and registering your adaptor, you can now call the custom function directly from MATLAB:
vid = videoinput('MyAdaptor', 1);
% Standard call
start(vid);
% Custom call
customCommand(vid, 'RESET_HARDWARE');
This integration also ensures that your hardware-specific functions behave like a native Image Acquisition Toolbox method, instead of relying on methods such as “loadlibrary” and “calllib”.
For further reference, kindly refer to the following official documentation:
I hope this helps!

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!