Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: C++ and Matlab
Date: Fri, 6 Nov 2009 16:14:02 +0000 (UTC)
Organization: Boeing
Lines: 10
Message-ID: <hd1i0a$i0j$1@fred.mathworks.com>
References: <79efd49c-346d-40b2-a663-67f754b7b8da@g23g2000yqh.googlegroups.com> <7fa345d3-dbfa-4afd-b79d-4ffe72c6192c@g23g2000yqh.googlegroups.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1257524042 18451 172.30.248.35 (6 Nov 2009 16:14:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 6 Nov 2009 16:14:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:583049


Izzy <iskandarprins@gmail.com> wrote in message <7fa345d3-dbfa-4afd-b79d-4ffe72c6192c@g23g2000yqh.googlegroups.com>...
> 
> Aha, thanks. So basically whenever I send in data to and from matlab a
> copy is made thus is invalidates the usage of a singleton.

From a practical standpoint, yes. From a technical standpoint, you *could* create your singleton using mxMalloc etc memory allocation functions and make that memory part of an mxArray that was returned to MATLAB. MATLAB will not actually copy the data area of the variable you return. But once you *change* the data area on the MATLAB side MATLAB will create a new data area for the changed results and break the connection between the MATLAB variable and your singleton. So it amounts to the same thing. 

To get the functionality of your singleton on the MATLAB side you would have to resort to calling the mex file for data access and data setting. I.e., have your singleton at the top level so it maintains its state across multiple mex calls. Then when you want the value, call the mex routine with a special input (e.g., a string 'get') that will cause the mex function to return a copy of the singleton as an mxArray. If you want to change the state of the singleton, then create the changed state on the MATLAB side and call the mex routine with this new state and another special input (e.g., a string 'set') that sets the singleton value. Not a real clean solution, but you could get it to work.

James Tursa