我有一個gui,當我點擊它們時工作正常,但不是點擊我想通過在鍵盤上按一個鍵(如uparrow)來激活它們。我確實在第一個按鈕(C3)上創建了一個keypressfnc。但我不知道該怎麼辦now.Here是我的GUI代碼如何將鍵分配給Matlab上的按鈕?
function varargout = untitled(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
sr = 16000;
T = 4; % seconds duration
t = 0:(1/sr):T;
n = 20;
f = ((2^(1/12))^(n-49))*440;
a = linspace(0,1,numel(t));
y = exp(-a*4).*sin(2*pi*f*t);
sound(y, sr);
plot(t, y);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
sr = 16000;
T = 4; % seconds duration
t = 0:(1/sr):T;
n = 40;
f = ((2^(1/12))^(n-49))*440;
a = linspace(0,1,numel(t));
y = exp(-a*4).*sin(2*pi*f*t);
sound(y, sr);
plot(t, y);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on key press with focus on pushbutton1 and none of its controls
% --- Executes on key press with focus on pushbutton1 and none of its controls.
% --- Executes on key press with focus on pushbutton1 and none of its controls.
function pushbutton1_KeyPressFcn(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata structure with the following fields (see UICONTROL)
% 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)
我是一個很初級的MATLAB student.I只是希望我的功能運行時我點擊uparrow.i無法理解你的例子對不起。我沒有(button = uicontrol())例如 – Wardruna 2014-11-23 13:04:11
我給出的例子是一個使用KeyPressFcn屬性執行功能的編程GUI。按下向上箭頭時,圖形背景將會改變顏色。要在GUIDE中執行相同的操作,您必須將KeyPressFcn添加到您希望執行KeyPressFcn的任何時候可能關注的對象。這基本上意味着將它添加到GUI中的每個對象,包括數字窗口本身。現在,您只需將它添加到您的按鈕中,這樣KeyPressFcn將僅在點擊按鈕後點擊向上箭頭鍵纔會執行。 – Staus 2014-11-23 23:21:49
因此,在GUIDE中執行起來要困難得多,因爲編程方式更容易。我現在更改了一下代碼,現在我的函數可以工作了。 – Wardruna 2014-11-24 08:32:47