Hello. I am trying to use the GUI to play back a pure tone based on the value entered in the edit box. I would like the pure tone to play at the frequency entered in the edit box, without having to manually define each elseif statement as I've done below with 300, 400, 500, and 250 Hz values:
% --- Executes on button press in pb_tone.
function pb_tone_Callback(hObject, eventdata, handles)
% hObject handle to pb_tone (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
t=0:1/44100:(1-1/44100);
fs=44100;;
freq=str2num(get(handles.edit_freq,'String'));
if freq==300
y=cos(2*pi*300*t);
sound(y,fs);
elseif freq==400
y=cos(2*pi*400*t);
sound(y,fs);
elseif freq==500
y=cos(2*pi*500*t);
sound(y,fs);
else
y=cos(2*pi*250*t);
sound(y,fs);
end
function edit_freq_Callback(hObject, eventdata, handles)
% hObject handle to edit_freq (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,'String') returns contents of edit_freq as text
% str2double(get(hObject,'String')) returns contents of edit_freq as a double
Any help would be very much appreciated. Thank you.

 채택된 답변

Jan
Jan 2018년 7월 12일

1 개 추천

y = cos(2 * pi * freq * t);
sound(y, fs);

추가 답변 (0개)

질문:

2018년 7월 12일

댓글:

2018년 7월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by