写一段matlab代码,在GUI界面实现选择时间的功能,代码必须能在matlab2016b上运行
时间: 2024-05-01 14:17:53 浏览: 83
GUI.m.zip_GUI_MATLAB GUI源代码_matlab 用户界面_starl54
好的,请稍等,我为您生成代码:
```matlab
function varargout = timeSelector(varargin)
% TIMESELECTOR MATLAB code for timeSelector.fig
% TIMESELECTOR, by itself, creates a new TIMESELECTOR or raises the existing
% singleton*.
%
% H = TIMESELECTOR returns the handle to a new TIMESELECTOR or the handle to
% the existing singleton*.
%
% TIMESELECTOR('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TIMESELECTOR.M with the given input arguments.
%
% TIMESELECTOR('Property','Value',...) creates a new TIMESELECTOR or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before timeSelector_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to timeSelector_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help timeSelector
% Last Modified by GUIDE v2.5 08-Apr-2022 20:32:51
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @timeSelector_OpeningFcn, ...
'gui_OutputFcn', @timeSelector_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 timeSelector is made visible.
function timeSelector_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to timeSelector (see VARARGIN)
% Choose default command line output for timeSelector
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes timeSelector wait for user response (see UIRESUME)
% uiwait(handles.figure1);
set(handles.popupmenu_hours, 'String', {'00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23'});
set(handles.popupmenu_minutes, 'String', {'00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51','52','53','54','55','56','57','58','59'});
set(handles.popupmenu_am_pm, 'String', {'AM','PM'});
% --- Outputs from this function are returned to the command line.
function varargout = timeSelector_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;
function popupmenu_hours_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu_hours (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_hours contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu_hours
updateTime(handles);
function popupmenu_minutes_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu_minutes (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_minutes contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu_minutes
updateTime(handles);
function popupmenu_am_pm_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu_am_pm (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_am_pm contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu_am_pm
updateTime(handles);
function updateTime(handles)
% get the selection from popupmenus
hours = handles.popupmenu_hours.String{handles.popupmenu_hours.Value};
minutes = handles.popupmenu_minutes.String{handles.popupmenu_minutes.Value};
am_pm = handles.popupmenu_am_pm.String{handles.popupmenu_am_pm.Value};
% format the time according to the user selection
if strcmp(am_pm, 'PM')
hours = num2str(str2num(hours)+12);
end
time = [hours ':' minutes ' ' am_pm];
% update the time label
handles.text_time.String = time;
% --- Executes during object creation, after setting all properties.
function popupmenu_hours_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu_hours (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes during object creation, after setting all properties.
function popupmenu_minutes_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu_minutes (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes during object creation, after setting all properties.
function popupmenu_am_pm_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu_am_pm (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
```
请将上面的代码粘贴到一个新文件里并保存为 matlab 文件,在 MATLAB 中打开并运行该文件即可。运行后,您将看到带有 GUI 界面的时间选择器。您可以使用下拉菜单来选择小时,分钟和 AM / PM,时间将在下面的文本框中显示。
阅读全文