MATLAB Object-Oriented Programming

(Joyce) #1

function makeGraph(obj)
rg = obj.Range;
x = min(rg):max(rg);
data = obj.FuncHandle(x);
plot(data)
end
end
methods (Static)
function obj = loadobj(s)
if isstruct(s)
fh = s.FuncHandle;
rg = s.Range;
obj = GraphExpression(fh,rg);
else
makeGraph(s);
obj = s;
end
end
end
end


Save and Load Object


Create an object with an anonymous function and a range of data as inputs:


h = GraphExpression(@(x)x.^4,[1:25])


h =


GraphExpression with properties:


FuncHandle: @(x)x.^4
Range: [1x25 double]


Save the GraphExpression object and close the graph:


save myFile h
close


Load the object. MATLAB recreates the graph:


load myFile h


If the load function cannot create the object and passes a struct to loadobj, loadobj
attempts to create an object with the data supplied.


Basic saveobj and loadobj Pattern
Free download pdf