i beginner in matlab , learn following question online , having trouble solving it. have 1 x 20 matrix called current_load
need update periodically. matrix resides in main workspace of matlab (as shown in code bellow).
current_loads = zeros(1, 20); col=1:20 current_loads(1,col)=10; %// give nodes current value of 10 end object = handleobject(current_load);%to pass reference recursive_remove(object, 0);
in order pass current_load
reference, have created following class handleobject.m
classdef handleobject < handle properties object=[]; end methods function obj=handleobject(receivedobject) obj.object=receivedobject; end end end
this matrix passed function call recursive_remove
(shown bellow).
function recursive_remove( current_load, val ) current_load.object = new matrix; if(val<10) current_load.object(1,3) = 2+val; %not correct way of using current_load ?? end recursive_remove( current_load, current_load.object (1,3) ) end
intention here modify current_load
variable in function , later can see these same changes main. code doesn't work since don't know how pass reference. need pass reference since calling recursively without returning main make overwrite variable @ caller. please show example if possible.
if need feature, can turning handleobject class singleton this:
classdef handleobject < handle properties object=[]; end methods(static) function obj = instance(receivedobject) persistent uniqueinstance try if isempty(uniqueinstance) obj = handleobject(receivedobject); uniqueinstance = obj; else obj = uniqueinstance; end catch me disp(me.getreport); end end end methods function obj=handleobject(receivedobject) obj.object=receivedobject; end end end
your recursive function become bit simpler then:
function recursive_remove(val ) current_load = handleobject.instance(); current_load.object = new matrix; if(val<10) current_load.object(1,3) = 2+val; end recursive_remove(current_load.object (1,3) ) end
and create instance of handleobject class, like:
object = handleobject.instance(current_load);
hope helps further.