我有一个 GUI,它从 *.xls 文件中导入数据并绘制图形。我不能做的最后一件事是数据刷新,因为我的 xls 文件每 15 秒更新一次。我希望我的 GUI 每 20 秒更新一次输入数据。我怎样才能做到这一点?而且我还想要一个能够暂停/运行数据更新的按钮。
function viewSTUEP
global hPlot hChoice raw
handles.F = figure('Name','viewSTUEP', 'Position',[100 20 900 550], ...
'Color',[0.8 0.8 0.8], 'NumberTitle','off', 'Resize','off');
uicontrol('Style','pushbutton', 'Position',[690 510 94 30], ...
'FontSize',12, 'String','Load', 'Callback',@LoadData);
uicontrol('Style','pushbutton', 'Position',[790 510 94 30], ...
'FontSize',12, 'String','Save plot', 'Callback',@SaveAs);
uicontrol('Style', 'pushbutton', 'Position', [690 475 94 30], ...
'FontSize',12, 'String','Update', 'Callback',@UpdatePlot);
uicontrol('Style','pushbutton', 'Position',[790 475 94 30], ...
'FontSize',12, 'String','Close', 'Callback',{@Close,handles});
set(handles.F, 'CloseRequestFcn', {@Close,handles})
set(gcf, 'Toolbar', 'figure');
clear raw, raw(1,:) = {''};
hPlot = axes('Position',[0.06 0.08 0.68 0.9], 'Visible','on');
uicontrol('Style','text', 'Position',[690 432 130 35], ...
'BackgroundColor',[0.8 0.8 0.8], 'FontSize',12, ...
'HorizontalAlignment','left', 'String','Columns for plot:');
hChoice = uicontrol('Style','listbox', 'Position',[690 420 204 22], ...
'FontSize',12, 'String',raw(1,:), 'Min',1, 'Max',1);
function Close(hObject, eventdata, handles)
delete(handles.F);
function LoadData(hObject, eventdata)
global hChoice raw xdat
[fname, pname] = uigetfile('*.xls','Select a data file');
if fname ~= 0
filename = strcat(pname, fname);
[numeric,text,raw] = xlsread(filename);
num = length(raw(1,:));
if (num > 21) num = 21; end
posY = 442 - (20*num);
set(hChoice, 'String',raw(1,:));
set(hChoice, 'Max',num);
set(hChoice, 'Position',[690 posY 204 (20*num)]);
xdat = datenum(0,0,0,0,0,cell2mat(raw(2:end,6)));
end
function UpdatePlot(hObject, eventdata)
global hPlot hChoice raw xdat
if ~isempty(raw)
cols = get(hChoice,'Value');
plot(hPlot, xdat,cell2mat(raw(2:end,cols(1))), 'LineWidth',2), datetick('x','HH:MM')
grid on
if (length(cols) > 1)
colors = 'brgcmyk'; c = 2;
hold(hPlot, 'on')
for k = cols(2:end)
plot(hPlot, xdat,cell2mat(raw(2:end,k)),colors(c),'LineWidth',2)
c = c+1; if (c > 7) c = 1; end
end
hold(hPlot, 'off')
end
legend(hPlot, raw(1,cols), 'Interpreter','none','Location','SouthEast')
xlabel(hPlot, 'Time in minutes:');
ylabel(hPlot, 'Temperature in C:');
end
function SaveAs(hObject, eventdata, handles)
[Save,savename] = uiputfile('*.eps','Save plot as: ')
fname=fullfile(savename,Save);
print('-depsc','-r300','-noui',fname)