How to share memory between C and MATLAB?

24 views (last 30 days)
ag
ag on 12 Jun 2013
I have developed a DLL in C language. I have 3 functions and a viriable in that DLL. I was able to load the library and also call the function in matlab. I have a C application which also uses the same DLL. But if the variable(in DLL) value is changed in C application its not reflected in MATLAB and vice-versa. Can anyone tell me how memory sharing between C and MATLAB can be achieved? So that if i change the value of a variable in library using C its changed value should be reflected in MATLAB and vice-versa.

Answers (1)

James Tursa
James Tursa on 12 Jun 2013
Your C application and your MATLAB program are separate processes with their own separate address spaces. When each pulls in the DLL they do so within their own address spaces. Even though they are running the same "code" the address space the code is using is different for each.
The only thing on the FEX I am aware of related to this is the following submission by Joshua Dillon, which shares matrix data between two MATLAB sessions:
and this submission:
  6 Comments
ag
ag on 14 Jun 2013
Edited: ag on 14 Jun 2013
Ok.So there is no way to achieve what i m trying to achieve? Or is there any other way to make MATLAB and C code to share a common piece of memory?
James Tursa
James Tursa on 15 Jun 2013
Edited: James Tursa on 15 Jun 2013
The easy way to do this is to turn your C application into a mex function. I.e., load it as a dll into MATLAB. That way it will share the same address space as MATLAB and can access the same shared memory. The harder way is using something like Joshua Dillon's submission noted above.

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!