MATLAB: I2C object creation in Simulink

aardvarki2cmatlab coders-functionssimulink

Hi,
im trying to create an i2c object in simulink for reading an sensor. In Matlab every thing works fine and i can read the data out of the sensor :
mpu_6050 = i2c('aardvark',0,hex2dec('68'));
fopen(mpu_6050);
fwrite(mpu_6050,65);
y = fread(mpu_6050,1);
fclose(mpu_6050);
When i try to add an Matlab-Function-Block to Simulink:
function y = mpu6050_read()
mpu_6050 = i2c('aardvark',0,hex2dec('68'));
fopen(mpu_6050);
fwrite(mpu_6050,65);
y = fread(mpu_6050,1,'*char');
fclose(mpu_6050);
end
i will get this error:
The 'i2c' class does not support code generation.
Is it really not possible to use the i2c class in simulink or do i have to use it in another way?
Regards V.Sadiku

Best Answer

The MATLAB Function block always generates C-code from the MATLAB code for faster execution. If you intend to run the block only for simulation, you can continue to use functions unsupported for code-generation by declaring them as extrinsic functions. I don't think fopen, fwrite, fread and fclose are supposed for code-generation either, so please declare all of them as extrinsic. You will also need to pre-allocate the output of fread, 'y', as described in this previously answered question.
Related Question