Adding UI control to pause a dynamic plot/ simualtion.
5 ビュー (過去 30 日間)
古いコメントを表示
I want to add a pause button to a dynamically updating plots.
The basic idea of my program is:
- function main() loads dataset and calls for the next function.
- Next function initialises all the variables and figures and inside the for loop I call the function that does all the work called processLidar()
- Inside processLidar() I do all the grunt work of the Lidar data. Also inside this function I have couple of for loops that keep updating the plot. At the beginning of this function I have " uicontrol("Position", [5, 5, 60 ,30], "String", "Pause", "Callback", @pauseCallback) "
The pauseCallback function is as follows:
function pauseCallback(pushButton, eventData)
if (get(pushButton, "String") == "Pause")
set(pushButton, "String", "Resume");
uiwait;
else
set(pushButton, "String", "Pause");
uiresume;
end
end
However this does not work, I keep clicking the pause button on the figure to no avail.
0 件のコメント
回答 (1 件)
Shubham
2023 年 4 月 3 日
Hi Megharaj,
It sounds like you are trying to add a pause button to a real-time updating plot that is generated within a loop. The issue you are facing is that the pause button does not work. One possible reason for this is that the uiwait function within the pauseCallback function is blocking the execution of your main loop.
To resolve this issue, try using a timer object in MATLAB to control the update rate of your plot. Here's an example code that demonstrates this approach:
function main()
% Load data and initialize variables
data = load('mydata.mat');
x = data.x;
y = data.y;
t = data.t;
idx = 1;
% Create figure and axes for the plot
fig = figure;
ax = axes('Parent', fig);
% Create pause button
uicontrol('Parent', fig, 'Style', 'pushbutton', ...
'String', 'Pause', 'Callback', @pauseCallback, ...
'Position', [5, 5, 60, 30]);
% Create timer object
timerObj = timer('ExecutionMode', 'fixedRate', ...
'Period', 0.1, 'TimerFcn', @updatePlot);
% Start timer
start(timerObj);
% Nested functions
function updatePlot(~, ~)
% Update plot
plot(ax, x(1:idx), y(1:idx));
xlabel(ax, 'X');
ylabel(ax, 'Y');
title(ax, sprintf('Time: %0.1f s', t(idx)));
% Increment index
idx = idx + 1;
% Stop timer when end of data is reached
if idx > length(x)
stop(timerObj);
end
end
function pauseCallback(~, ~)
% Toggle timer object
if strcmp(timerObj.Running, 'on')
stop(timerObj);
set(gcf, 'Pointer', 'arrow');
else
start(timerObj);
set(gcf, 'Pointer', 'crosshair');
end
end
end
In this code, the updatePlot function is called by the timer object at a fixed rate (in this case, every 0.1 seconds). The pauseCallback function toggles the state of the timer object when the pause button is clicked. Note that the set(gcf, 'Pointer', ...) commands change the mouse pointer to indicate whether the plot is paused or running.
You can modify this example to fit your specific use case by replacing the x, y, and t variables with your own data and adjusting the update rate of the timer object to match the frequency of your Lidar data updates.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Interactive Control and Callbacks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!