MATLAB Object-Oriented Programming

(Joyce) #1

Here is the fcneval.isSuitable method:


function isOk = isSuitable(funcH)
v = [1 1;1 1];
% Can the expression except 2 numeric inputs
try
funcH(v,v);
catch %#ok
me = MException('DocExample:fcneval',...
['The function ',func2str(funcH),' Is not a suitable F(x,y)']);
isOk = me;
return
end
% Does the expression return non-scalar data
if isscalar(funcH(v,v));
me = MException('DocExample:fcneval',...
['The function ',func2str(funcH),'' Returns a scalar when evaluated']);
isOk = me;
return
end
isOk = [];
end


The fcneval.isSuitable method could provide additional test to ensure that the
expression assigned to the FofXY property meets the criteria required by the class
design.


Other Approaches


The class could have implemented a property set event for the FofXY property and
would, therefore, not need to call notify (see “Listen for Changes to Property Values” on
page 11-40). Defining a class event provides more flexibility in this case because you can
better control event triggering.


For example, suppose that you wanted to update the graph only if the new data is
different. If the new expression produced the same data within some tolerance, the
set.FofXY method could not trigger the event and avoid updating the graph. However,
the method could still set the property to the new value.


Listener and Callback for UpdateGraph Event


The fcnview class creates a listener for the UpdateGraph event using the
addlistener method:


obj.HLUpdateGraph = addlistener(obj.FcnObject,'UpdateGraph',...
@(src,evnt)listenUpdateGraph(obj,src,evnt)); % Add obj to argument list


Techniques for Using Events and Listeners
Free download pdf