Matlab OOP: Create a handle class that (inherits from) is also a double? -


i create class inherits (and acts) double handle. example:

a     = myhandledoubleclass();   % instance of handle double. a.val = 1;                       % set value of 1. b     =                        % assign b value of                                  % but, b double, not object >> b  b =      1 

now if change value of (for example set a.val = 2) b should change has same value (or more correctly both , b in effect pointers same value).

is possible in matlab somehow?

edit: further clarify, these handle objects should later on used normal doubles , need act such access operations (for example b=a, b=a(ind) etc. without having explicitly access value property (its ok use property field set value, a.val = 1). purpose have double object used in many places (other objects, including patches) , have change value in 1 place (and other references update automatically due handle property).

example:

one application this, crate patch vertices of double class

f = [1 2 3]; v = mydoubles();  patch( 'faces', f, 'vertices', v )  v.values = updatevalues(); drawnow 

so whenever change values @ 1 point, patch updates values without having explicitly go in , change vertices field.

writing own pointer class

if need pointer double, can have double parameter of class. here's simple example based on code using pointers in matlab

first define class inherits handle class, matlab's pointer class. class properties store double.

classdef handleobject < handle    properties       object=0; % double    end     methods       function obj=handleobject(receivedobject) %this constructor          obj.object=receivedobject;       end    end end 

then can write example above as,

a = handleobject(1); %the constructor passes 1 object property b = a;   display(b.object); % should print 1 

to access value of a , b, need use a.object or b.object.

to clear, can use handleobject construct pointer matlab object type, not doubles. if call, h = handleobject("hello world"), store string in h.object.

using pointers other matlab classes

if use method matlab object patch run risk matlab copies values in handleobject.object rather using pointer.

for example,

v_array = [0 0;1 0;1 1;0 1]; % x , y vertex coordinates f_array = [1 2 3 4]; % vertices connect make square v = handleobject(v_array); % make vertex pointer f = handleobject(f_array); % make face pointer patch( 'faces', f.object, 'vertices', v.object, 'facecolor','red');  v.object = [1 .5; 1 1; .5 1]; % let's use pointer change vertices f.object = [1 2 3]; % , faces triangle drawnow; % don't see change in figure 

the reason don't see change because patch copied values own variable rather storing pointer. far know, there no way force patch use our pointers instead of copying value, there alternative: use pointer patch object change values.

v_array = [0 0;1 0;1 1;0 1]; f_array = [1 2 3 4]; p = patch('faces',f_array,'vertices',v_array,'facecolor','red'); %the patch method optionally returns handle index h = handle(p); % handle  % have pointer patch object can use change property h.faces = [1 2 3]; % such number of faces h.vertices = [1 .5; 1 1; .5 1]; % , location of vertices h.facecolor = 'green'; % color drawnow 

many matlab methods, such patch optionally return pointers objects create, allowing change properties on fly. other methods provide handles useful classes. use gca method example, change properties of plot's axes.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -