View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
InplaceArray: a semi-pointer package for Matlab

5.0 | 2 ratings Rate this file 13 Downloads (last 30 days) File Size: 10.9 KB File ID: #24576 Version: 1.5

InplaceArray: a semi-pointer package for Matlab


Bruno Luong (view profile)


28 Jun 2009 (Updated )

Manipulate (multi-dimensional) arrays in the in-place form

| Watch this File

File Information

This package helps to create "children" arrays that share the same data as the mother array. The children array can be shaped any customized size for later manipulation.

In short it provides some sort of equivalent C-"pointer" where Matlab lacks.

Extreme cautious when using this package, otherwise encountered crashes and memory leaks warranty. For "advanced" users only.

SPARSE, STRUCTURE are currently not supported

ATTENTION: Serious bug in testinplace.m in the first submission. Before the new fixed version is submitted please do not run this function (the rest is OK)


This file inspired Sharedmatrix and Mx Array Definition.

MATLAB release MATLAB 7.8 (R2009a)
Other requirements mex setup Tested on 2006B-2009B
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
26 May 2011 Binlin Wu

Josh, thank you for your emails. The code is working now for vectors. It's not critical if it works for scalar or not.

This is a great program and will be very helpful.

13 Feb 2011 Andrew

Hi Bruno. Can I suggest adding the line:

((Internal_mxArray*)(BMAT))->reserved3 = 1; /*give it a reference count*/

Around about line 194 of inplacearray.c. This will tell Matlab not to try to free the memory pointer so:

A = eye(3);
B = inplacecolumn(A, 2);
clear B;

will not cause Matlab to crash (R2009A anyway)
--Cheers, Andrew

Comment only
20 Dec 2010 Joshua Dillon

Hi Bruno. I confirm this problem for my sharedmatrix submission. It would seem Mathworks has dropped support for array_access_inlining. Sadly, this is probably the beginning of the end for our hacks. Please let me know if you find a method to automatically reverse engineer future implementations of the mxArray object (and I will do the same). --Cheers, Josh

Comment only
24 Nov 2010 Bruno Luong

Bruno Luong (view profile)

Edit: save the following in Internal_mxArray.h before building the mex file:

Matlab version: 2010B

typedef struct {
void *reserved;
int reserved1[2];
void *reserved2;
size_t number_of_dims;
unsigned int reserved3;
struct {
unsigned int flag0 : 1;
unsigned int flag1 : 1;
unsigned int flag2 : 1;
unsigned int flag3 : 1;
unsigned int flag4 : 1;
unsigned int flag5 : 1;
unsigned int flag6 : 1;
unsigned int flag7 : 1;
unsigned int flag7a: 1;
unsigned int flag8 : 1;
unsigned int flag9 : 1;
unsigned int flag10 : 1;
unsigned int flag11 : 4;
unsigned int flag12 : 8;
unsigned int flag13 : 8;
} flags;
size_t reserved4[2];
union {
struct {
void *pdata;
void *pimag_data;
void *reserved5;
size_t reserved6[3];
} number_array;
} data;
} Internal_mxArray;

Comment only
19 Nov 2010 Bruno Luong

Bruno Luong (view profile)

There is a compilation issue with 2010B, and I still not able to find a good fix. A workaround is compile by hand the mex files:

% On 32-bit platform
mex inplacecolumnmex.c
mex releaseinplace.c

or on 64-bit platform
mex -largeArrayDims inplacecolumnmex.c
mex -largeArrayDims releaseinplace.c

Comment only
06 Aug 2010 Joshua Dillon

I have figured out how to work with sparse matrices. Contact me if interested.

Comment only
06 Aug 2010 Joshua Dillon

Wow this really is a gem!

Any ideas how to handle sparse matrices? ...nothing in matrix.h sadly.

15 Dec 2009 Bruno Luong

Bruno Luong (view profile)

Hi Christophe,
Thank you for report this. It is expected that PACK would mess up with memory pointers.

Comment only
11 Dec 2009 Christophe Lauwerys

Nice, however, very tricky.
For instance PACK messes up the reference and I have the feeling more things can go wrong when you use memory-related function calls, or am I being too pessimistic?

>> A=1
>> B=inPlaceArray(A);
>> B=2;
>> pack
>> A
A =

Comment only
29 Jun 2009 1.1

Correct a serious bug in testinplace.m (could cause computer crash)

29 Jun 2009 1.3

Important Comment

01 Jul 2009 1.4

Upload testinplace.m file with modifications reported previously (submission 2 & 3 upload wrong file, sorry)

18 Jul 2012 1.5

Improve the installation

Contact us