2

i'm in seek of information. Me and a other students like me have to create sound in Matlab. We create them, and we have to create also an interactif interface to play those sound.

So we create a piano, and when we click on a key, it's play the sound ( that is the function. )

We also wanted that we can push a key on the Keyboard that call the function. We heard about KeyPressFCN, but we don't know how to use it, because when we search every tutorial, they didn't give enough information about it.

So, when we rightclick on the element we want, and them we call KeyPressFCN, what is the next step ? What did we have to do to "put" the function on this KeyPressFCN.

For example, to make one of the sound, we have :

% --- 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);
4

1 回答 1

13

实际上我只是在引用 Matlab 文档和帮助。

  1. 如果您使用 GUIDE 右键单击​​您的图形(而不是任何对象)>> View Callbacks >> 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!
    

    玩弄你的键盘并查看事件数据。显然,当您键入时,该图形必须处于活动状态。

  2. 如果您使用的是 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.
    
  3. 或者,如果您不想使用内联函数:

    fig_h = figure;
    set(fig_h,'KeyPressFcn', @key_pressed_fcn);
    

    然后定义你的key_pressed_fcn,如:(创建一个新的mfile,名称为:key_pressed_fcn.m,当然你可以使用你想要的任何名称,但与上面的KeyPressFcn名称相同)

    function key_pressed_fcn(fig_obj,eventDat)
    
    get(fig_obj, 'CurrentKey')
    get(fig_obj, 'CurrentCharacter')
    get(fig_obj, 'CurrentModifier')
    
    % or 
    
    disp(eventDat)
    
  4. 或者!使用脚本作为 KeyPressFcn 回调函数

    fig_h = figure;
    set(fig_h,'KeyPressFcn', 'key_pressed');
    

    然后编写 key_pressed 脚本:

    get(fig_h, 'CurrentKey')
    get(fig_h, 'CurrentCharacter')
    get(fig_h, 'CurrentModifier')
    

有关 Matlab 帮助,请参阅:http: //www.mathworks.com/help/matlab/ref/figure_props.html中的“KeyPressFcn 事件结构”

于 2013-05-17T17:25:25.943 回答