我想用MATLAB用户界面创建一个日期时间向量,到目前为止我已经创建了所有编辑框和按钮,它们工作正常,但我面临着两个小问题
1)pushbutton_cb3函数仅在我按下日期输入时才从编辑骨骼中检索日期字符串,我该如何删除?
2)我想函数pushbutton_cb3返回我在代码中提到的某个值,函数waitfor
看起来很不错但我不知道如何在这里使用它
帮助将受到高度赞赏
function AddClassCalender
f = figure('Name', 'AddClassCalender');
EditBoxHandle1 = uicontrol(f, 'Style', 'Edit','Tag','edit1',...
'Units','normalized','Position', [0.05 0.9 0.2 0.05], 'Callback',@edit_callback);
EditBoxHandle2 = uicontrol(f, 'Style', 'Edit','Tag','edit2', ...
'Units','normalized','Position', [0.3 0.9 0.2 0.05], 'Callback',@edit_callback2);
ButtonHandle1 = uicontrol(f, 'Style', 'PushButton', ...
'String', 'Select the starting date', ...
'Units','normalized','Position', [0.05 0.85 0.22 0.05], ...
'callback', @pushbutton_cb1);
ButtonHandle2 = uicontrol(f, 'Style', 'PushButton', ...
'String', 'Select the starting date', ...
'Units','normalized','Position', [0.3 0.85 0.22 0.05], ...
'callback', @pushbutton_cb2);
TextHandle = uicontrol(f, 'Style', 'Text', ...
'String', 'Interval(mins):', ...
'Horizontalalignment', 'left', ...
'Units','normalized','Position', [0.6 0.9 0.2 0.05]);
EditBoxHandle3 = uicontrol(f, 'Style', 'Edit','Tag','edit3', ...
'Units','normalized','Position', [0.7 0.9 0.2 0.05], 'Callback',@edit_callback3);
ButtonHandle3 = uicontrol(f, 'Style', 'PushButton', ...
'String', 'Generate', ...
'Units','normalized','Position', [0.7 0.85 0.2 0.05], ...
'callback', @pushbutton_cb3);
%% I WANT THE PUSHBUTTON TO RETURN THE DATETIME VECTOR HERE SO I CAN USE IT FURTHER MORE
uiwait(f)
function pushbutton_cb1(hcbo, eventStruct)
uicalendar('Weekend', [1 0 0 0 0 0 1], ...
'SelectionType', 1, ...
'DestinationUI', EditBoxHandle1);
end
function pushbutton_cb2(hcbo, eventStruct)
uicalendar('Weekend', [1 0 0 0 0 0 1], ...
'SelectionType', 1, ...
'DestinationUI', EditBoxHandle2);
end
function edit_callback(hcbo, eventStruct)
Date = hcbo.String;
hcbo.UserData = Date;
end
function edit_callback2(hcbo, eventStruct)
Date = hcbo.String;
hcbo.UserData = Date
end
function edit_callback3(hcbo, eventStruct)
Interval = hcbo.String;
hcbo.UserData = Interval
end
function pushbutton_cb3(hcbo, eventStruct)
date1 = findobj('Tag','edit1');
date2 = findobj('Tag','edit2');
interval = findobj('Tag','edit3');
data1 = date1.UserData
data2 = date2.UserData
int_str = interval.UserData
interv = str2num(int_str)
interval_cal = 1/(1440/interv);
datevector = datetime(data1,'Format','dd-MMM-yyyy HH:mm:ss'):interval_cal:datetime(data2)
end
答案 0 :(得分:0)
我没有金融工具箱,所以让我在我的例子中考虑我手动输入字段中的日期(我检查以确保输入与uicalendar相同的字符串)。
您可以使用waitfor
代替uiwait
。这样,只有当例外变量被更改时,应用程序才会继续(在我的例子中,我将数据放在ButtonHandle3.UserData
function [returnedVector] = AddClassCalender
f = figure('Name', 'AddClassCalender');
EditBoxHandle1 = uicontrol(f, 'Style', 'Edit','Tag','edit1',...
'Units','normalized','Position', [0.05 0.9 0.2 0.05], 'Callback',@edit_callback);
EditBoxHandle2 = uicontrol(f, 'Style', 'Edit','Tag','edit2', ...
'Units','normalized','Position', [0.3 0.9 0.2 0.05], 'Callback',@edit_callback2);
ButtonHandle1 = uicontrol(f, 'Style', 'PushButton', ...
'String', 'Select the starting date', ...
'Units','normalized','Position', [0.05 0.85 0.22 0.05], ...
'callback', @pushbutton_cb1);
ButtonHandle2 = uicontrol(f, 'Style', 'PushButton', ...
'String', 'Select the starting date', ...
'Units','normalized','Position', [0.3 0.85 0.22 0.05], ...
'callback', @pushbutton_cb2);
TextHandle = uicontrol(f, 'Style', 'Text', ...
'String', 'Interval(mins):', ...
'Horizontalalignment', 'left', ...
'Units','normalized','Position', [0.6 0.9 0.2 0.05]);
EditBoxHandle3 = uicontrol(f, 'Style', 'Edit','Tag','edit3', ...
'Units','normalized','Position', [0.7 0.9 0.2 0.05], 'Callback',@edit_callback3);
ButtonHandle3 = uicontrol(f, 'Style', 'PushButton', ...
'String', 'Generate', ...
'Units','normalized','Position', [0.7 0.85 0.2 0.05], ...
'callback', @pushbutton_cb3);
%% I WANT THE PUSHBUTTON TO RETURN THE DATETIME VECTOR HERE SO I CAN USE IT FURTHER MORE
waitfor(ButtonHandle3, 'UserData')
% Do what you want with the data here, I choose to return it and to close the figure;
returnedVector = ButtonHandle3.UserData;
delete(f);
function pushbutton_cb1(hcbo, eventStruct)
uicalendar('Weekend', [1 0 0 0 0 0 1], ...
'SelectionType', 1, ...
'DestinationUI', EditBoxHandle1);
end
function pushbutton_cb2(hcbo, eventStruct)
uicalendar('Weekend', [1 0 0 0 0 0 1], ...
'SelectionType', 1, ...
'DestinationUI', EditBoxHandle2);
end
function edit_callback(hcbo, eventStruct)
Date = hcbo.String;
hcbo.UserData = Date;
end
function edit_callback2(hcbo, eventStruct)
Date = hcbo.String;
hcbo.UserData = Date
end
function edit_callback3(hcbo, eventStruct)
Interval = hcbo.String;
hcbo.UserData = Interval
end
function pushbutton_cb3(hcbo, eventStruct)
date1 = findobj('Tag','edit1');
date2 = findobj('Tag','edit2');
interval = findobj('Tag','edit3');
data1 = date1.UserData;
data2 = date2.UserData;
int_str = interval.UserData;
interv = str2num(int_str);
interval_cal = 1/(1440/interv);
datevector = datetime(data1,'Format','dd-MMM-yyyy HH:mm:ss'):interval_cal:datetime(data2);
% I set the returned data in UserData in order to finish the waitfor
hcbo.UserData = datevector;
end
end