The class may change, but the method names are the same.
classdef BaseClass < handle properties p = [] end methods function h = get_save_handle( obj ) h = @obj.save; end endendclassdef Class1 < BaseClass properties p1 = [] end methods function save( obj ) % ...
end endendclassdef Class2 < BaseClass properties p2 = [] end methods function save( obj ) % ... end endend
I need to change the callback of a save button in my UI depending on the user's data, which will determine which class is used. So if I'm using class1, the ClickedCallback of my save button needs to be class1.save(), etc. Here is how I assign the callback:
switch data_type case 'Class1' save_data = Class1(); case 'Class2' save_data = Class2();endsaveUIButton.set( 'ClickedCallback', { save_data.get_save_handle(), f }, 'UserData', save_data ) % 'f' is my figure.
But when I click the save button, the varargin{:} causes an error because the 'save' function arguments aren't compatible:
Error using Class1/saveToo many input arguments.Error in BaseClass>@(varargin)obj.save(varargin{:}) (line 82) h = @obj.save; Error while evaluating PushTool ClickedCallback.
Best Answer