How to use a "global static" variable in matlab function called in c

Hi, I am currently encoding MATLAB and C. I compiled MATLAB functions into a C library using MATLAB Compiler (mcc), and called functions in a shared library in a C ++ program.

Is it possible to declare a global variable for exchanging data between MATLAB functions when called in C ++?

More precisely, if matlab has a function matlabA()and a function matlabB()and is compiled into a C ++ shared library using the mcc compiler as cppA()well cppB(), can I share a variable between them simply by declaring the variables as global in matlabA()and matlabB()?

It doesn't seem to work, then how can I share a variable between functions?

Thank!

MATLAB

function matlabA()
    global foo
    foo = 1;
end

function matlabB()
    global foo
    foo
end

C ++

cppA();
cppB();
+5
source share
1 answer

According to this Loren Shure blog post, it is strongly discouraged to use non-stationary static variables (such as global read / write tables) in deployed applications.

Instead, you can create a processing class to encapsulate data and explicitly pass the object to these functions (which has reference copy semantics).

Example:

FooData.m

classdef FooData < handle
    properties
        val
    end
end

fun_A.m

function foo = fun_A()
    foo = FooData();
    foo.val = 1;
end

fun_B.m

function fun_B(foo)
    disp(foo.val)
end
+2
source

All Articles