Matlab Codegen -- cast mxArray output of extrinsic function to a structure or cell array with unknown size or field count

7 Ansichten (letzte 30 Tage)
Dear Matlab Community,
When using Matlab Codegen, the output of an extrinsic function seems always to be a generic mxArray data type. The Matlab Codegen documentation page states, that to use an mxArray data type for further Matlab-level calculations, it has to be casted to a Matlab data type by declaring it beforehand, e.g. as following:
coder.extrinsic( 'foo' ); % declare function foo as extrinsic (mex-file, not m-file)
out = 0.0; % declare out data type as double, initialise with value 0
out = foo(); % out is returned as mxArray and directly casted to double
Imagine an external third-party function myMexFunc, which returns a structure with flexible field names and data sizes, and (unfortunately) is already precompiled as .mex64 by the developer (i.e. not available as Matlab source code).
In pure Matlab code, the output of such function can be directly returned to a variable, and then parsed step by step depending on the available field names and values. Unfortunately, this does not work in Matlab Codegen.
Let's assume some dummy function with similar behaviour ...
function out = myMexFunc( )
info = {{ 'time', 's', 'wave', 'v' }};
time = ( 0 : 0.1 : 10 );
wave = sin( 2*pi*time );
out = struct( 'info', info, 'time', time, 'wave', wave );
end
... and compile it to .mex64 with codegen ...
codegen myMexFunc.m -o myMexFunc
This creates a myMexFunc.mex64 file, which can now be called as a function.
( Now, let's remove the myMexFunc.m from scope, else Matlab may refer to m-code, and not the compiled mex-code. )
Now, let's assume a top-level function myMainFunc calling the mex-code above:
function myMainFunc()
% coder.extrinsic( 'myMexFunc' ); % extrinsic declaration necessary for codegen
% out = struct( 'info', {{ '', '', '', '' }}, 'time', [], 'wave', [] );
out = myMexFunc( );
disp( ' out = ' ); disp( out );
if isfield( out, 'info' )
disp( ' out.info = ' ); disp( out.info );
end
end
In a pure Matlab call, this works without issues. Should we now compile myMainFunc using
codegen myMainFunc.m -o myMainFunc
an error message indicates, that myMexFunc is missing or undefined.
This is solved by declaring the mex-function as extrinsic (uncomment line #2 with coder.extrinsic declaration). Reattempt to compile, the error message changes to
??? Attempt to extract field 'info' from 'mxArray'.
... which means, that for codegen the out variable is not a Matlab structure anymore, but an mxArray data type. Accessing its fields is not possible.
According to the documentation page referenced above, this is solved by declaring the variable type before the call to an extrinsic function (uncomment line #4), which results in ...
??? Incorrect size for expression 'myMexFunc.info{1}': expected [1x0] but found [1x4]
..., which is again explainable: the declared out.info{1} field is an empty 0x0 char string, while mex-file function returns a non-empty value.
Should we declare the .info field with exact element sizes, the same error type occurs for numeric data fields. Declaring the latter with a fixed size is not possible, since the data size is not known in advance.
To this behaviour the following questions arise:
Q1: Can the out variable or its fields be declared to receive the data of any size, not just of the same size?
Q2: Is there any documentation on mxArray data type usage in Matlab code? Could it be possible to keep the out variable as mxArray, and read its native fields or call its native methods to indirectly access the stored data?
Q3: Or could it be posible to indirectly read the field count, sizes, names and/or whatever else from mxArray data type, prepare the corresponding out data type with 'correct' data sizes, and then cast mxArray to it?
PS.: Native Matlab disp() is capable of displaying mxArray data type with its top-level fields and elements, and can be compiled with codegen. Perhaps there's another similar built-in capable of accessing the fields?
Any ideas on the topic are welcome!
Best regards,
Sergey

Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by