Automatically Update Plots on GUI With Multiple Sliders
8 views (last 30 days)
Show older comments
Hi There,
I am building a gui to model a number of different signals (sine, square, triangle, sawtooth). These signals can be controlled by sliders which vary their frequency, total time, phase and sample rate. You must also be able to change between signals using a dropdown menu.
The signals will then be displayed on four different graphs, the first axis showing the original signal, and the rest showing the graphs being maniuplated via fourier transforms.
The graphs should all update simultaneously when each slider is used. I am using callback functions and currently the graphs will only update when the code to plot them is indside the callback function that corresponds to that individual slider. For example if the code for the sin function is inside the slider that varies the frequency, the code will only update when the frequency slider is moved. It will still read the values from the other sliders, but the graph displayed will only update when the frequency slider is moved.
So my question is - is there some way to like callback functions so they cause the plot to update simultaneously?
Thanks
0 Comments
Accepted Answer
Stephen23
on 16 Oct 2020
Edited: Stephen23
on 16 Oct 2020
One simple approach is to have one "update" function in your GUI and call it from each individual callback:
function slider1CallBack(o,e)
.. % get new value, store (e.g. via guidata, nested function)
.. % do whatever else
updateAll()
end
function slider2CallBack(o,e)
.. % get new value, store (e.g. via guidata, nested function)
.. % do whatever else
updateAll()
end
.. etc.
function updateAll()
.. % update all signal plots here
end
Based on this we might also note that because every callback is basically the same we could replace them all with just one callback and an extra input which simply specifies which graphics object has triggered the callback:
set(object1,'Callback',{@oneCallback,1})
set(object2,'Callback',{@oneCallback,2})
..
function oneCallback(o,e,idx);
.. % decide what to do based on idx (e.g. switch)
.. % update all signals
end
Of course storing lots of separate numbered variables is not a good use of MATLAB, in your actual code you should be using one graphics handles array.
See my FEX submission cubehelix_view for a working example of multiple sliders updating plotted data:
3 Comments
Rik
on 16 Oct 2020
You need to make sure the guidata variable is available to that function. You can either pass it as an input argument of the updateAll function or use handles=guidata(gcbf);.
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!