return empty array in c++ mexFunction in the out-of-process mexHost

4 visualizaciones (últimos 30 días)
Vincent Huber
Vincent Huber el 2 de Oct. de 2020
Respondida: Samay Sagar el 26 de Sept. de 2024
I want to use the out-of-process functionnality and to return an empty array in my C++ mexFunction.
It is not exactly the same question as :
since it is in C++ and with feval.
Here the problem:
void MexFunction::operator()(mm::ArgumentList outputs, mm::ArgumentList inputs)
{
outputs[0] = f.createEmptyArray();
}
that i called with
>> mh = mexhost; res = feval(mh,'example');
Error using matlab.mex.MexHost/feval
Too many output arguments.
but directly called via the Command window I have:
>> example
ans =
[]

Respuestas (1)

Samay Sagar
Samay Sagar el 26 de Sept. de 2024
I have also encountered this issue in the past while returning an empty array from a C++ MEX function. I have verified that this issue was fixed in MATLAB R2021a.
As a workaround, you can return a non-empty value like a string literal (say "empty_array") instead of an empty array, which can be later handled in MATLAB to replace the value with [ ].
Another workaround is to catch the error in MATLAB and handle it there to use [ ].
mh = matlab.mex.MexHost();
try
res = feval(mh, "example");
catch ME
switch ME.identifier
case 'MATLAB:maxlhs' % The error when empty array is returned
res = [];
case 'MATLAB:mex:MexHostCrashed' % A general MEX error
clear mex;
otherwise
rethrow(ME)
end
end

Categorías

Más información sobre MATLAB Compiler SDK en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by