how to avoid copying script with multiple sliders in GUI
이전 댓글 표시
Hi there! Very new user to Matlab here. I have created a very simple Siine plot with two control sliders. In order to get the sliders to work, I have to copy and paste the code into each slider callback:
function horiz_slider_Callback(hObject, eventdata, handles)
% hObject handle to horiz_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
time= 0:1/500:2;
A = get(handles.horiz_slider, 'Value');
F = get(handles.vertical_slider, 'Value');
MySignal = A*sin(2*pi*F*time);
plot(handles.axes1, time, MySignal);
I have tried to use a function and recall the code for the vertical_slider but cannot figure out how to do it, so I have to paste the same stuff in:
function vertical_slider_Callback(hObject, eventdata, handles)
% hObject handle to vertical_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
time= 0:1/500:2;
A = get(handles.horiz_slider, 'Value');
F = get(handles.vertical_slider, 'Value');
MySignal = A*sin(2*pi*F*time);
plot(handles.axes1, time, MySignal);
Very grateful of an easy peasy example to follow please! Cheers all!
댓글 수: 11
Manikanta Aditya
2024년 3월 24일
You can avoid duplicating code by creating a separate function that updates the plot. This function can be called from both slider callbacks.
function update_plot(handles)
time= 0:1/500:2;
A = get(handles.horiz_slider, 'Value');
F = get(handles.vertical_slider, 'Value');
MySignal = A*sin(2*pi*F*time);
plot(handles.axes1, time, MySignal);
end
function horiz_slider_Callback(hObject, eventdata, handles)
update_plot(handles);
end
function vertical_slider_Callback(hObject, eventdata, handles)
update_plot(handles);
end
Luke Cabot
2024년 3월 24일
Voss
2024년 3월 24일
Another option would be to make both sliders have the same callback function.
Luke Cabot
2024년 3월 24일
Luke Cabot
2024년 3월 24일
Voss
2024년 3월 24일
Remove the "end" from both slider callback functions and from update_plot.
Luke Cabot
2024년 3월 24일
Voss
2024년 3월 24일
An example of both sliders having the same callback would be: edit the sliders' callbacks in GUIDE - set them both to the same function; then define that function in your m-file.
Regarding the current error, can you upload both the m-file and the fig-file here using the paperclip button?
Luke Cabot
2024년 3월 24일
Voss
2024년 3월 24일
These run fine for me, so I think you have it sorted.
Manikanta Aditya
2024년 3월 25일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!