use*_*530 2 matlab matlab-guide
我正在寻求信息.我和像我一样的其他学生必须在Matlab中创建声音.我们创建它们,我们还必须创建一个交互界面来播放这些声音.
所以我们创建了一个钢琴,当我们点击一个键时,它会播放声音(即功能).
我们还希望我们可以在调用该函数的键盘上按下一个键.我们听说过KeyPressFCN,但我们不知道如何使用它,因为当我们搜索每个教程时,他们没有提供足够的信息.
因此,当我们右键单击我们想要的元素时,我们调用KeyPressFCN,下一步是什么?我们要做什么来将"功能""放"在这个KeyPressFCN上.
例如,要制作其中一个声音,我们有:
% --- Execution lors d'un appui sur le bouton Do (première blanche)
function pushbutton1_Callback(hObject, eventdata, handles)
octave = str2double(get(handles.zone1,'String'));
frequence = 2093; %--- Fréquence initialement Do6
frequence2 = frequence./ octave;
son = sin(2*pi*frequence2*(0:0.000125:0.2));
sound(son);
Run Code Online (Sandbox Code Playgroud)
pm8*_*m89 13
其实我只是在引用Matlab文档和帮助.
如果您使用GUIDE右键单击您的图形(不在任何对象上)>>查看回调>> KeyPressFcn,则它将自动生成以下函数:
function figure1_KeyPressFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata structure with the following fields (see FIGURE)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles structure with handles and user data (see GUIDATA)
% add this part as an experiment and see what happens!
eventdata % Let's see the KeyPress event data
disp(eventdata.Key) % Let's display the key, for fun!
Run Code Online (Sandbox Code Playgroud)
使用键盘玩游戏并查看eventdata.显然,当你打字时,这个数字必须是活跃的.
如果你正在使用uicontrol(而不是GUIDE)这是制作gui的程序化方法
(使用内联函数)
fig_h = figure; % Open the figure and put the figure handle in fig_h
set(fig_h,'KeyPressFcn',@(fig_obj,eventDat) disp(['You just pressed: ' eventDat.Key]));
% or again use the whole eventDat.Character or eventDat.Modifier if you want.
Run Code Online (Sandbox Code Playgroud)或者,如果您不想使用内联函数:
fig_h = figure;
set(fig_h,'KeyPressFcn', @key_pressed_fcn);
Run Code Online (Sandbox Code Playgroud)
然后定义你的key_pressed_fcn :(创建一个名为key_pressed_fcn.m的新mfile,当然你可以使用你想要的任何名称,但与上面的KeyPressFcn名称相同)
function key_pressed_fcn(fig_obj,eventDat)
get(fig_obj, 'CurrentKey')
get(fig_obj, 'CurrentCharacter')
get(fig_obj, 'CurrentModifier')
% or
disp(eventDat)
Run Code Online (Sandbox Code Playgroud)要么!使用脚本作为KeyPressFcn回调函数
fig_h = figure;
set(fig_h,'KeyPressFcn', 'key_pressed');
Run Code Online (Sandbox Code Playgroud)
然后编写key_pressed脚本:
get(fig_h, 'CurrentKey')
get(fig_h, 'CurrentCharacter')
get(fig_h, 'CurrentModifier')
Run Code Online (Sandbox Code Playgroud)有关Matlab帮助,请参阅以下网址中的 "KeyPressFcn事件结构":http: //www.mathworks.com/help/matlab/ref/figure_props.html