matlab mex in-place editing via "mxUnshareArray" or similar functionality

3 views (last 30 days)
Hello,
I'm trying to pass the reference for a matlab array and modify that array in place within the mex function. It seems that Matlab used to have this functionality, but that it was deprecated in 2015b. Currently, the input prhs[n] is temporary in scope, and changes that I make to that pointer are not reflected in matlab. My current implementation uses: mxComplexDouble * output_array = mxGetComplexDoubles(plhs[0]); then populates the output_array; and at the close of each mex call, it seems a relatively expensive copy operation is being performed.
It seems matlab used to support the capability to pass by reference and unshare the reference if modifications were needed. Is there still any way to do this? https://undocumentedmatlab.com/articles/matlab-mex-in-place-editing
vectorization in matlab is much slower than the C mex function which uses fixed-point representation to speed up calculations
i'm working to pre-allocate the output array now, but i would still really like to be able to write directly to the output array via the C mex function

Answers (1)

James Tursa
James Tursa on 14 Dec 2023
Edited: James Tursa on 14 Dec 2023
So, MATLAB has never officially supported modifying input variables in-place in a mex routine. There are potential side effects of modifying shared variables. There are/were some undocumented API functions and hacks that could let you do this in an unofficial manner to try and avoid the side effects, and maybe that is what you are referring to. But many of these undocumented functions have disappeared from the API or are prevented from linking with your code, so that is harder to do currently.
You will need to post a small example that reproduces your problem. I am unaware of anything that would prevent you from modifying a prhs[ ] variable in-place as long as you accept the risk of side effects. Your post mentions plhs[ ] and not prhs[ ], so I am unsure what you are really trying to do here.

Categories

Find more on MATLAB Compiler 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!