put a .m file into the simulink ,why can't work?

1 view (last 30 days)
I pulled a m function model and code as followed
function y = fcn(u1,u2)
coder.extrinsic('loadlibrary','libfunctions','calllib','unloadlibrary');
loadlibrary('DLL_ADD','DLL_ADD.h');
libfunctions DLL_ADD;
G=calllib('DLL_ADD','Add',u1,u2);
unloadlibrary('DLL_ADD');
y = G;
but there are some errors I can't figure out
Function output 'y' cannot be an mxArray in this context. Consider preinitializing the output variable with a known type.
Function 'MATLAB Function1' (#65.0.232), line 1, column 1:
"function y = fcn(u1,u2)"
Launch diagnostic report.
Errors occurred during parsing of MATLAB function 'MATLAB Function1'(#65)

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 16 Apr 2013
Since you have declared calllib as an extrinsic function using coder.extrinsic, Simulink is unable to determine the type of 'y' which is required to generate C code from the MATLAB code, for execution. You can preinitialize the size and type of 'y' as the error suggests.
For example, assuming that 'y' is the same size and type as 'u1':
function y = fcn(u1,u2)
coder.extrinsic('loadlibrary','libfunctions','calllib','unloadlibrary');
loadlibrary('DLL_ADD','DLL_ADD.h');
libfunctions DLL_ADD;
G=calllib('DLL_ADD','Add',u1,u2);
unloadlibrary('DLL_ADD');
y = coder.nullcopy(zeros(size(u1)));
y = G;
  3 Comments
Kaustubha Govind
Kaustubha Govind on 19 Apr 2013
euphy: Could you please accept my answer if it resolved the issue? Thanks!
Peter Fakhry
Peter Fakhry on 18 Mar 2017
excuse me, in my simulink model i am using matrices imported from workspace but now i want to use these matrices from text files so i can generate a .exe file which automatically grabs these text files and use them in simulation. i used matlab function block and wrote a function containing 'textscan' and 'cell2mat' functions but it doesnt work. do you know how to do it? thanks

Sign in to comment.

More Answers (1)

pardis ghaziamin
pardis ghaziamin on 2 Aug 2018
function e = fcn(teta,p1,p2) %#codegen
p1=[x_r;y_r;teta_r]; p2=[x;y;teta];
e=[cos(teta),sin(teta),0;-sin(teta),cos(teta),0;0,0,1]*[(x_r)-x;(y_r)-y;(teta_r)-teta];
it has Launch diagnostic report. on x_r ,x

Community Treasure Hunt

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

Start Hunting!