Why does axes() not return an Object but a graphics handle?
Show older comments
Hi, guys.
The graphics system in MATLAB puzzles me, here take axes for example.
What's the class name of axes object? Can users subclass it?
Why does axes() not return an axes object, but a handle?
Graphics handle is some like index/pointer/reference of a graphics object. For C Language, one can allocate a block of memory in a function and return the pointer. But I have not find that Matlab Language can implement such thing. If true, how does axes() allocate the memory for axes object and return a handle? I think the axes object should have disappeared when returned from axes
I think it is the function of MATLAB Software, not of the Matlab Language that makes axes object alive after returned from axes().
Any idea?
Accepted Answer
More Answers (2)
Daniel Shub
on 17 Nov 2011
4 votes
MATLAB has some inconsistencies which stem from the fact that originally MATLAB only had one class (everything was stored in what would commonly be called a double). Graphics commands returned a handle to an underlying object (which was hidden from the user). The handle of course was a double. Then MATLAB introduced some different data classes (obvious data types like single, logical, etc) as well as things like cells, structures and function handles. Somewhere along the way MATLAB introduce OOP, and then later redesigned the OOP system. Graphics objects have yet to get the overhaul they deserve and need. There are rumors and indications in the code that this is changing and that someday we MATLAB may have graphics objects that behave like other objects.
3 Comments
Shunchao Wu
on 18 Nov 2011
Daniel Shub
on 18 Nov 2011
The official documentation:
http://www.mathworks.co.uk/help/techdoc/creating_plots/f7-20419.html
A blog by Loren about doubles being the only data type:
http://blogs.mathworks.com/loren/2010/04/01/double-or-nothing-no-joking/
Yair has a ton of stuff related to this:
http://undocumentedmatlab.com/blog/matlab-hg2/
http://undocumentedmatlab.com/blog/new-information-on-hg2/
http://undocumentedmatlab.com/blog/handle2struct-struct2handle-and-matlab-8/
Shunchao Wu
on 18 Nov 2011
Titus Edelhofer
on 17 Nov 2011
0 votes
Hi,
with the function axes you create an object of type axes (similar to a constructor). The handle returned may be viewed as a reference to the object (you can copy the handle, clear the handle variable etc and the object remains unaffected). The axes object lives until either the figure it lives in dies, or it's deleted (delete(axes_handle)).
Titus
13 Comments
Shunchao Wu
on 17 Nov 2011
Titus Edelhofer
on 17 Nov 2011
If you know this, then I don't understand what the question is ;-).
Titus Edelhofer
on 17 Nov 2011
And how this relates to your question on pointers some days ago...
Shunchao Wu
on 17 Nov 2011
Shunchao Wu
on 17 Nov 2011
Titus Edelhofer
on 17 Nov 2011
Hi Shunchao,
regarding inplace, MATLAB does inplace operation for the following type of functions: (a) the function has input and output with the same name: function X = someFun(a,X,b). (b) It is called with the same variable, i.e., x = someFun(42, x, 23). If both is fulfilled, x is passed inplace (i.e., X in someFun is not a copy of x but a reference). In mex files something similar is possible but strongly discouraged because of hard to control side effects.
Shunchao Wu
on 17 Nov 2011
Titus Edelhofer
on 17 Nov 2011
8KB (plus of course a few bytes). I just tried with x=zeros(5000); and had "only" 200MB peak in the task manager. Note though, that the code with x=fun1(x) has to be in some other function, that will not work if you type it directly at the command prompt. I did the following:
function fun2
X = zeros(5000);
pause(2);
X = fun1(X);
end
Inside fun1 I added a pause as well to be sure that I see any peak.
Shunchao Wu
on 17 Nov 2011
Titus Edelhofer
on 18 Nov 2011
hmm, which version of MATLAB are you using? The inplace optimization was added somewhere around R2009a (but don't remember exactly).
Shunchao Wu
on 18 Nov 2011
Titus Edelhofer
on 18 Nov 2011
Sorry, I did read wrong. As I said: from command line you will not see the inplace behaviour. It works only for a function called from within another function (in my example fun2 calling fun1).
Shunchao Wu
on 18 Nov 2011
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!