Automatically Update Plots on GUI With Multiple Sliders

17 views (last 30 days)
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

Accepted Answer

Stephen23
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
Samuel Leeney
Samuel Leeney on 16 Oct 2020
I am just wondering - when using your solution I am now unable to use axes(handles.AxesNumber) to chose which of my four axes the graphs will be plotted into. Is there some way around this?
I get the following error message:
Unable to resolve the name handles.FFTAxes.
Error in Signal_UI>updateAll (line 261)
axes(handles.FFTAxes)
Error in Signal_UI>Frequency_Callback (line 83)
updateAll()
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Signal_UI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Signal_UI('Frequency_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Rik
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);.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!