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?

 채택된 답변

Walter Roberson
Walter Roberson 2011년 2월 10일

1 개 추천

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

추가 답변 (2개)

Jan
Jan 2011년 2월 9일

1 개 추천

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

noga cohen
noga cohen 2011년 2월 9일
I'm still having problems with this.. my @yourCallback function updates the graph, but it doesn't wait for a button press to update the following graph. so the graphs are presented sequentially without stopping.
I have a for loop that creates the graph on each cycle. if I put the "PushButton = uicontrol..." inside the loop, the index does not change and on every press the same graph is created.
I have some basic misunderstanding... it should be simple...
Jan
Jan 2011년 2월 9일
@noga: Please post the relevant part of the code.
noga cohen
noga cohen 2011년 2월 10일
PushButton = uicontrol(gcf,'Style', 'push', 'String', 'Next', ... 'Position', [300 10 30 30], ...
'CallBack', @GraphCreator);
function GraphCreator(PushButton, EventData)
for i = 1:N
load data(i)
create graph(i)
end
K VdB
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
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.

댓글을 달려면 로그인하십시오.

Doug Hull
Doug Hull 2011년 2월 9일

0 개 추천

You need to make a function that will update your graph. Once you have done that, set the callback of the button to that function.
What you have now, when you click the button, the pause button is run. This should not do much of anything to help you.

카테고리

도움말 센터File Exchange에서 Toolbox Distribution에 대해 자세히 알아보기

질문:

2011년 2월 9일

댓글:

2016년 10월 24일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by