Products & Services Solutions Academia Support User Community Company

eml.opaque - Declare variable in generated code

Syntax

y = eml.opaque(type,[value]);

Arguments

y

Specifies the variable declared in the generated code.

type

Specifies the data type. Specify a C type defined in the user include file to avoid compilation errors. The C type provided must support assignment in C.

    Note   Arrays in C cannot be directly assigned, for example, the type declaration int[50] is not valid.

value (optional)

Specifies the data value declaration. Specify a C expression not dependent on Embedded MATLAB variables or functions.

If you do not define an initial value, you must initialize the value before using the data. Using the data without prior assignment can lead to compilation errors.

Description

y = eml.opaque(type,[value]); declares data of the type type, and the optional initial value value. eml.opaque allows you to manipulate C data that the Embedded MATLAB subset does not recognize. type and value are treated as strings by Embedded MATLAB functions. Data initialized with eml.opaque can be:

eml.opaque declares the type of a variable; it does not instantiate the variable. You can instantiate a variable using eml.ceval after declaring the variable type with eml.opaque. For example:

% Declare fp1 of type FILE *
fp1 = eml.opaque('FILE *');

%Create the variable fp1
fp1 = eml.ceval('fopen', cstring('filetest.m'), cstring('r'));

Example

The following example uses eml.opaque to declare a variable f as a FILE * type.

% This example returns its own source code by using 
% fopen/fread/fclose.
function buffer = filetest

% Declare 'f' as an opaque type 'FILE *'
f = eml.opaque('FILE *', 'NULL');
% Open file in binary mode
f = eml.ceval('fopen', cstring('filetest.m'), cstring('rb'));

% Read from file until end of file is reached and put
% contents into buffer
n = int32(1);
i = int32(1);
buffer = char(zeros(1,8192));
while n > 0
    % By default, Embedded MATLAB converts all constant values
    % to doubles, so explicit type conversion to in32 is inserted.
    n = eml.ceval('fread', eml.ref(buffer(i)), int32(1), ...
        int32(numel(buffer)), f);
    i = i + n;
end
eml.ceval('fclose',f);

buffer = strip_cr(buffer);

% Put a C termination character '\0' at the end of MATLAB string
function y = cstring(x)
    y = [x char(0)];

% Remove all character 13 (CR) but keep character 10 (LF)
function buffer = strip_cr(buffer)
j = 1;
for i = 1:numel(buffer)
    if buffer(i) ~= char(13)
        buffer(j) = buffer(i);
        j = j + 1;
    end
end
buffer(i) = 0;

See Also

eml.ceval, eml.ref, eml.rref, eml.wref

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS