필터 지우기
필터 지우기

Automatically Update Plots on GUI With Multiple Sliders

조회 수: 17 (최근 30일)
Samuel Leeney
Samuel Leeney 2020년 10월 16일
댓글: Rik 2020년 10월 16일
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

채택된 답변

Stephen23
Stephen23 2020년 10월 16일
편집: Stephen23 2020년 10월 16일
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
Samuel Leeney
Samuel Leeney 2020년 10월 16일
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 2020년 10월 16일
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);.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by