How do I program a callback on a UICONTROL?
26 次查看(过去 30 天)
显示 更早的评论
I want to present several graphs such that when I click a push button beside a graph, the next in the series appears.
I tried the following code, but the push button those not influence the running.
PushButton = uicontrol(gcf,'Style', 'pushbutton', 'String', 'Next',
'Position', [300 10 30 30], 'Callback', 'pause');
How should I do this?
0 个评论
采纳的回答
Walter Roberson
2011-2-10
More than what you asked for...
PushButton = uicontrol(gcf, 'Style', 'push', 'String', 'Next', ...
'Position', [300 10 30 30], ...
'CallBack', @GraphCreator, ...
'UserData', 0);
function GraphCreator(PushButton, EventData)
newiter = get(PushButton, 'UserData') + 1;
if newiter > N
wd = warndlg('Reached end of graphs. Push again to start over');
uiwait(wd, 30); %30 second timeout on warning
if ishandle(wd) %only true if timed out
delete(wd)
end
set(PushButton, 'UserData', 0);
return
end
set(PushButton, 'UserData', newiter);
InputData = load(FileName{newiter});
fn = fieldnames(InputData);
thisdataname = fn{1};
create_graph(S.(thisdataname));
end
0 个评论
更多回答(2 个)
Jan
2011-2-9
Try this at first:
PushButton = uicontrol(gcf,'Style', 'pushbutton', ...
'String', 'Next', ...
'Position', [300 10 30 30], ...
'Callback', @yourCallback);
function yourCallback(ObjH, EventData)
% ObjH is the button handle
FigH = ancestor(ObjH, 'figure'); % or: gcbf
set(FigH, 'Color', rand);
Then insert something more useful in the callback function. BTW: Searching in the documentation is always a good point to start from:
docsearch callback
5 个评论
K VdB
2016-10-24
When I run the script noga cohen gave it says:
''All functions in a script must be closed with an 'end'.
When I close the script with an end it says:
''Function definitions are not permitted in this context.''
Walter Roberson
2016-10-24
Only R2016b and later permit function definitions inside a .m file that does not start with the word "function" or "classdef" .
Anyhow: store the function inside GraphCreator.m and have the assignment to PushButton in a different routine.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!