How to turn off code generation in Simulink? (function is not supported for standalone code generation)

Suzan Cioc picture Suzan Cioc · Jul 18, 2013 · Viewed 7.8k times · Source

I am creating a simulation which I will port to another language myself. So I don't need to use code generation capabilities of Simulink. How to turn it's attempts to allow code generation OFF?

For example, I have the following code inside MATLAB Function block:

function OutImage = ResizeCropPad(InImage, Width, Height)
%#codegen

    %coder.extrinsic('imresize');

    % resizing to defined height
    scale = Height/size(InImage,1);
    InImage = imresize(InImage, scale);

    % cropping to defined width
    if Width<size(InImage,2)
        padarray(InImage, [0 size(InImage,2)-Width], 0, 'both');
    elseif Width>size(InImage,2)
        b = floor((Width-size(InImage,2))/2);
        InImage = InImage(:,b:b+Width-1,:);
    end

    OutImage = InImage;

and it gives an error

The function 'imresize' is not supported for standalone code generation. See the documentation for coder.extrinsic to learn how you can use this function in simulation.

If I uncomment coder.extrinsic('imresize') line I get new 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.

Addressing line 11, with if.

Why? Is it possible to completely disable code generation, for example at model level?

Answer

am304 picture am304 · Jul 18, 2013

You need to uncomment the coder.extrinsic('imresize'); line and declare/initialise the InImage variable before calling the imresize function. See Converting mxArrays to Known Types for more info.

EDIT following discussion in the comments:

The following should work:

function OutImage = ResizeCropPad(InImage, Width, Height)
%#codegen

    coder.extrinsic('imresize');

    % resizing to defined height
    scale = Height/size(InImage,1);
    OutImage = InImage;
    OutImage = imresize(InImage, scale);

    % cropping to defined width
    if Width<size(OutImage,2)
        padarray(OutImage, [0 size(OutImage,2)-Width], 0, 'both');
    elseif Width>size(OutImage,2)
        b = floor((Width-size(outImage,2))/2);
        OutImage = OutImage(:,b:b+Width-1,:);
    end