There was once a document floating about the internet by Sergey Simakov. It was very terse , not especially descriptive but covered the bases. This was in my experience the most authoritive text on matlab GUI's. I suspect it still is ...
You're solving two/three problems :
Closure Problem
Nested functions resolve this problem.
function iterator = count(initial)
% Initialize
if ~exist('initial','var')
counter = 0
else
counter = initial
end
function varargout = next() % [1]
% Increment
counter = counter + 1
varargout = {counter} % [1]
end
iterator = @next
end
Note(s) :
- One may not simply return counter ! It must be wrapped in varargout or assigned to some other output variable.
Usage
counter = count(4) % Instantiate
number = counter() % Assignment
number =
5
Closed Scope + State problem
No ugly braces, worrying about scope, cell arrays of strings. If you need access to something it's under SELF no more FINDOBJ, USERDATA, SET/GETAPPDATA, GLOBAL nonsense.
classdef Figure < handle
properties
parent@double
button@double
label@double
counter@function_handle
end
methods
function self = Figure(initial)
self.counter = count(initial) % [1]
self.parent = figure('Position',[200,200,300,100])
self.button = uicontrol('String','Push', 'Callback', @self.up, 'Style', 'pushbutton', 'Units', 'normalized', 'Position', [0.05, 0.05, 0.9, 0.4])
self.label = uicontrol('String', self.counter(), 'Style', 'text', 'Units', 'normalized', 'Position', [0.05, 0.55, 0.9, 0.4])
end
function up(self,comp,data)
set(self.label,'String',self.counter())
end
end
end
Note(s):
- One uses the function listed in the Closure problem above
Usage :
f = Figure(4) % Instantiate
number = get(f.label,'String') % Assign
number =
5
You may prefer :
f.label.get('String') % Fails (f.label is double not handle, go figure)
h = handle(f.label) % Convert Double to handle
number = h.get('String') % Works
number =
5
function callbackFunction(object,eventdata,variable). You may be able to add multiple extra arguments I haven't tried, I make a struct with all of the variables used by my GUI and just use it as the third input argument. Hope that is relevant!