MATLAB: Coder Error with Simulink Embedded function using “fopen”: Expected either a logical, char… Found an mxArray )

expected doublefopenmatlab codermxarraysimulink

Embedded function: should check if the file is available to be written. If yes, file is written fid is a parameter u is an input
function Matlab_to_WM(u, fid)
coder.extrinsic('dlmwrite');
coder.extrinsic('fopen');
coder.extrinsic('fclose');
fid=fopen('D:\damien\Echange_donnees_System\Consigne_Retour_94A.txt', 'w');
if fid ~= -1 && fid ~= 2
dlmwrite('D:\damien\Echange_donnees_System\Consigne_Retour_94A.txt' , u , 'newline', 'pc');
end
fclose('all');
end
The result of "fopen" gives a mxArray (1 x 1) Then the test on fid value generate an error: "Expected either a logical, char, int, fi, single, or double. Found an mxArray. MxArrays are returned from calls to the MATLAB interpreter and are not supported inside expressions. They may only be used on the right-hand side of assignments and as arguments to extrinsic functions."
This function, was working with the 2007b version and don't work anymore with 2012a version. Then, how to test a mxArray or to extract the value(1 x 1) as a double?

Best Answer

I think you might need to pre-declare that fid is a double scalar. Does it work if you add "fid=0;" above the "fopen" line?