[App Designer] Delaying plot function with respect to 'ValueChanging' callback
2 次查看(过去 30 天)
显示 更早的评论
I'm making a simple app that plots a curve based the the value of a knob. The way I'm currently doing this is calling an updatePlot(app) function from the knob ValueChanging(app, event) callback function. This works fine.
My issue is that there are very many calls of the ValueChanging callback in quick succession, as the knob is being turned. I'd like for there not to be that many plot updates per second. I'm thinking about doing something like this.
- Detect that there's been a ValueChanging callback call.
- Determine if the last plot update has happened in less than the minimum wait time (t_w).
- If the last plot update happened sooner than t_w seconds ago then "queue up" a plotting action.
- After t_w seconds have elapsed since the last plot update then do another plot update with the most recent Knob value.
How might I accomplish this or is there a better way to accomplish what I'm trying to do?
1 个评论
Voss
2025-5-10
编辑:Voss
2025-5-10
"is there a better way to accomplish what I'm trying to do?"
Maybe. First, I'd look for ways to improve the speed of your updatePlot function. For example, it shouldn't call high-level graphics functions (plot, surf, fill) which generally create new objects (i.e., lines, surfaces, patches, etc.) in the axes, but instead it should update the properties of existing objects and, to the extent that it needs to make new objects, that should be done using low-level graphics functions (line, surface, patch, etc.). If updatePlot can be made fast enough to be called from every ValueChanging call without affecting the responsiveness of the UI, then you may not have to worry about the whole scheme where you keep track of when the last plot update was and all that.
回答(1 个)
Sameer
2025-5-12
Hi @Rudy
You can solve this issue by adding a simple debounce mechanism using a timer. The idea is to only update the plot if a minimum amount of time has passed since the last update. If not, you schedule a delayed update using the latest knob value.Here's how you can do it:
- Track the time of the last plot update.
- If the knob is turned again too soon, cancel any existing timer and start a new one.
- When the timer fires, update the plot using the most recent knob value.
Example Code:
% In your app properties
properties (Access = private)
LastUpdateTime datetime = datetime('now');
UpdateTimer timer;
LatestKnobValue double;
MinWaitTime double = 0.2; % seconds
end
% In the ValueChanging callback
function KnobValueChanging(app, event)
app.LatestKnobValue = event.Value;
nowTime = datetime('now');
elapsed = seconds(nowTime - app.LastUpdateTime);
if elapsed >= app.MinWaitTime
app.LastUpdateTime = nowTime;
updatePlot(app, app.LatestKnobValue);
else
if ~isempty(app.UpdateTimer) && isvalid(app.UpdateTimer)
stop(app.UpdateTimer);
delete(app.UpdateTimer);
end
app.UpdateTimer = timer( ...
'StartDelay', app.MinWaitTime - elapsed, ...
'TimerFcn', @(~,~)debouncedUpdate(app));
start(app.UpdateTimer);
end
end
% Timer callback
function debouncedUpdate(app)
app.LastUpdateTime = datetime('now');
updatePlot(app, app.LatestKnobValue);
end
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!