How do I pass information from a text box in the GUI to a variable in the .m file?
4 次查看(过去 30 天)
显示 更早的评论
MAX
2018-4-10
How do I pass information from a text box in the GUI to a variable in the .m file? Precisely speaking, I want to put music notes such as A,B,C,C#,D in the text box in the GUI. Once I do this I want these music notes to appear in a variable called song=[] in the .m file.
采纳的回答
Walter Roberson
2018-4-10
notestr = get(handles.textbox1, 'String');
note_struct = regexp(notestr, '(?<note>[ABCDEFG]#?)', 'names');
note_list = {note_struct.note};
Now note_list will be a cell array of character vectors, each one of A, B, C, D, E, F, G, or one of those followed by #. Commas and spaces and other characters are permitted in the input -- though the above would need touching up to be able to ignore spaces between a note and its following # .
16 个评论
MAX
2018-4-13
编辑:Walter Roberson
2018-4-13
I am new to matlab so I don't really get what youre saying. To make it clearer I will explain more.
I have a listbox with frequencies and the call back of this listbox is below:
index_selected = get(hObject,'Value');
list = get(hObject,'String');
item_selected = list{index_selected};
switch item_selected
case 'A'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:1];
F_A=440; %Frequency of note A is 440 Hz
A=sin(2*pi*F_A*t);
sound(A,Fs);
case 'C'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:1];
F_C=261.626; %Frequency of note A is 440 Hz
C=sin(2*pi*F_C*t);
sound(C,Fs);
case 'G'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:1];
F_G=391.995; %Frequency of note A is 440 Hz
G=sin(2*pi*F_G*t);
sound(G,Fs);
end
Now my question is: How should I pass the selected item from the listbox to a variable in the push button callback. And then if I press the push button the song must play.
Sorry for the long question.
Walter Roberson
2018-4-13
Remove all of the code from the 'switch' to the end. Replace it with
current_text = get(handles.textbox1, 'String');
current_text = [current_text, item_selected];
set(handles.textbox1, 'String', current_text');
This will take add whatever the user selected in the listbox to the end of the string in the edit box.
Once that is done, you still have the problem of being able to convert what is in the edit box to a stream of individual note choices so that you can loop through them using that switch code you have. The code I provided earlier takes care of that analysis. You would use something like
for note_idx = 1 : length(note_list)
note_selected = note_list{note_idx};
switch note_selected
case 'A'
...
case 'A#'
...
end
end
MAX
2018-4-13
编辑:Walter Roberson
2018-4-13
Thanks for the reply but I have another question.
I actually ditched the listbox and now I am only using the edit text box to type in the letters.
I can hear the sound when I press the pushbutton. The problem is that when I type more than one letter, its like all the letters are playing at the same time. I want the duration to be different. For example if I type ACG, I want A to play first, then C gets played, then G gets played.
Any solutions sir?
Below is the code for the pushbutton callback:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
notestr = get(handles.edit2, 'String');
note_struct = regexp(notestr, '(?<note>[ABCDEFG]#?)', 'names');
note_list = {note_struct.note};
for note_idx = 1 : length(note_list)
note_selected = note_list{note_idx};
switch note_selected
case 'A'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:1];
F_A=440; %Frequency of note A is 440 Hz
A=sin(2*pi*F_A*t);
sound(A,Fs);
case 'C'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:2];
F_C=261.626; %Frequency of note A is 440 Hz
C=sin(2*pi*F_C*t);
sound(C,Fs);
case 'G'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:3];
F_G=391.995; %Frequency of note A is 440 Hz
G=sin(2*pi*F_G*t);
sound(G,Fs);
end
end
Walter Roberson
2018-4-13
sound() starts the sound playing and then continues running the rest of the code while the hardware continues to play the sound. Then your loop encounters the second sound() call and starts playing that as well.
To play only one thing at a time you should use audioplayer() instead of sound(), and you should use playblocking()
MAX
2018-4-13
It worked !! Thankyou so much.
One more thing I wanted to ask was how can I remove the pause between the letters being played.
The sequence is ACG. It plays A first then there is a pause, then it plays C and then a pause again, then it plays G and then a pause again.
The pause is only short but is there a way to completely remove it?
Walter Roberson
2018-4-13
The best way to remove the pause is to calculate all of the sound data first, put it all together in a vector, and then playblocking() the entire vector.
complete_sound = [];
for ... %loop over notes
switch
...
complete_sound = [complete_sound, A];
%don't play it yet
...
end %end of switch
end %end of loop over notes
playblocking(complete_sound);
MAX
2018-4-13
编辑:Walter Roberson
2018-4-13
What is wrong with the code below:
notestr = get(handles.edit1, 'String');
note_struct = regexp(notestr, '(?<note>[ABCDEFG]#?)', 'names');
note_list = {note_struct.note};
complete_sound = [];
for note_idx = 1 : length(note_list)
note_selected = note_list{note_idx};
switch note_selected
case 'A'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.3];
F_A=440; %Frequency of note A is 440 Hz
A=(sin(2*pi*F_A*t));
v=audioplayer(A,Fs);
complete_sound = [complete_sound,A];
case 'C'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.3];
F_C=261.626; %Frequency of note A is 440 Hz
C=sin(2*pi*F_C*t);
k=audioplayer(C,Fs);
complete_sound = [complete_sound,C];
case 'G'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.3];
F_G=391.995; %Frequency of note A is 440 Hz
G=sin(2*pi*F_G*t);
o=audioplayer(G,Fs)
complete_sound = [complete_sound,G];
case'F'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.3];
F_F=349.228; %Frequency of note A is 440 Hz
F=sin(2*pi*F_F*t);
i=audioplayer(F,Fs);
complete_sound = [complete_sound,F];
case 'D'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.3];
F_D=293.665; %Frequency of note A is 440 Hz
D=sin(2*pi*F_D*t);
j=audioplayer(D,Fs);
complete_sound = [complete_sound,D];
case 'E'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.3];
F_E=329.628; %Frequency of note A is 440 Hz
E=sin(2*pi*F_E*t);
m=audioplayer(E,Fs);
complete_sound = [complete_sound,E];
end
end
play(complete_sound);
playblocking(complete_sound);
Walter Roberson
2018-4-13
You used the same comment about the frequency of A each place ;-)
Do not audioplayer() each place. Just create one audioplayer at the end,
p = audioplayer(complete_sound, Fs);
playblocking(p);
MAX
2018-4-15
编辑:Walter Roberson
2018-4-15
Sir I want to call another pushbutton that pauses the song that is being played. I researched about it and tried some stuff but I couldn't do it. Any solutions?
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
notestr = get(handles.edit1, 'String');
note_struct = regexp(notestr, '(?<note>[ABCDEFG]#?)', 'names');
note_list = {note_struct.note};
complete_sound = [];
for note_idx = 1 : length(note_list)
note_selected = note_list{note_idx};
switch note_selected
case 'A'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.5];
F_A=440; %Frequency of note A is 440 Hz
A=(sin(2*pi*F_A*t));
complete_sound = [complete_sound,A];
case 'C'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.5];
F_C=261.626; %Frequency of note A is 440 Hz
C=sin(2*pi*F_C*t);
complete_sound = [complete_sound,C];
case 'G'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.5];
F_G=391.995; %Frequency of note A is 440 Hz
G=sin(2*pi*F_G*t);
complete_sound = [complete_sound,G];
case'F'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.5];
F_F=349.228; %Frequency of note A is 440 Hz
F=sin(2*pi*F_F*t);
complete_sound = [complete_sound,F];
case 'D'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.5];
F_D=293.665; %Frequency of note A is 440 Hz
D=sin(2*pi*F_D*t);
complete_sound = [complete_sound,D];
case 'E'
Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.5];
F_E=329.628; %Frequency of note A is 440 Hz
E=sin(2*pi*F_E*t);
complete_sound = [complete_sound,E];
end
end
h=audioplayer(complete_sound,Fs);
playblocking(h)
Walter Roberson
2018-4-15
Pausing is a bit more complicated. To do that you will need to switch from playblocking(h) to play(h) . You will also need to make h available to callbacks, somehow:
http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
In the callback for the pause button you should first check to see whether the shared variable has been set, and if it has then isvalid() to see if it names an object that still exists. If it is then isplaying() to see if it is currently playing; if it is then pause() the audioplayer object. When the user wants to resume, go back through the tests again (because the user might have triggered the deletion of the audio player) and use play() to resume.
MAX
2018-4-15
I actually make a different function:
function songmaker(hObject, handles)
Below this function I have the normal code with the for loop and other codes.
I have 4 pushbuttons to play, resume, pause, stop the song. Below is the code.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global player;
songmaker(hObject, handles);
play(player);
guidata(hObject, handles)
For the pause button callback I just changed from play(player) to pause(player). And similarly for the resume and stop pushbutton.
It works but my problem is for the 'resume' pushbutton. When I press the pause button the song stops. But when I press the resume button the whole song starts from the beginning.
How do I resume the song from where it stopped?
MAX
2018-4-15
In the resume callback function I tried the below code but the song plays from the start again.
global player;
songmaker(hObject, handles);
resume(player);
guidata(hObject, handles)
How do I get it to start from where it stopped?
Walter Roberson
2018-4-15
编辑:Walter Roberson
2018-4-15
Don't call songmaker again. You only need to call songmaker to build the sound vector.
songmaker should not construct the audioplayer object. The play callback should test to see if there is a valid audioplayer already and if not then construct it. Then it should start playing. The other callbacks should still test that the audioplayer is still valid but should not be constructing the audioplayer, should only be using the one out of the play callback.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)