我有一个显示Matlab的CPU使用率的代码,我的问题是如何随着时间的推移PLOT CPU使用率,因为这段代码不会在工作区中保存任何变量,我需要一种方法来存储CPU的使用情况。代码运行时的数组,以便我可以绘制它 谢谢你的时间。 这是代码:
function hcol = CPU_monitor
h = create_gui;
end
function mon = createMonitor
MatlabProcess = System.Diagnostics.Process.GetCurrentProcess(); %// "Matlab" process
cpuIdleProcess = 'Idle';
mon.NumOfCPU = double(System.Environment.ProcessorCount);
mon.ProcPerfCounter.Matlab = System.Diagnostics.PerformanceCounter('Process', '% Processor Time', MatlabProcess.ProcessName);
mon.ProcPerfCounter.cpuIdle = System.Diagnostics.PerformanceCounter('Process', '% Processor Time', cpuIdleProcess);
end
function updateMeasure(obj,evt,hfig)
h = guidata(hfig);
%// Calculate the cpu usage
cpu.total = 100 - h.mon.ProcPerfCounter.cpuIdle.NextValue / h.mon.NumOfCPU;
cpu.matlab = h.mon.ProcPerfCounter.Matlab.NextValue / h.mon.NumOfCPU;
%// update the display
set(h.txtTotalCPU,'String',num2str(cpu.total,'%5.2f %%'))
set(h.txtMatlabCPU,'String',num2str(cpu.matlab,'%5.2f %%'))
end
function StartMonitor(obj,evt)
h = guidata(obj);
start(h.t)
end
function StopMonitor(obj,evt)
h = guidata(obj);
stop(h.t)
end
function h = create_gui %// The boring part
h.fig = figure('Unit','Pixels','Position',[200 800 240 120],'MenuBar','none','Name','CPU usage %','NumberTitle','off');
h.btnStart = uicontrol('Callback',@StartMonitor,'Position',[10 80 100 30],'String', 'START');
h.btnStart = uicontrol('Callback',@StopMonitor,'Position',[130 80 100 30 ],'String', 'STOP');
h.lbl1 = uicontrol('HorizontalAlignment','right','Position',[10 50 100 20],'String','TOTAL :','Style','text');
h.txtTotalCPU = uicontrol('Position',[130 50 100 20],'String','0','Style','text');
h.lbl2 = uicontrol('HorizontalAlignment','right','Position',[10 10 100 20],'String','Matlab :','Style','text');
h.txtMatlabCPU = uicontrol('Position',[130 10 100 20],'String','0','Style','text');
movegui(h.fig,'center')
%// create the monitor
h.mon = createMonitor;
%// Create the timer
h.t = timer;
h.t.Period = 0.25;
h.t.ExecutionMode = 'fixedRate';
h.t.TimerFcn = {@updateMeasure,h.fig};
h.t.TasksToExecute = Inf;
%// store the handle collection
guidata(h.fig,h)
end
答案 0 :(得分:1)
一种方法是将变量保存到文件中,并在停止后读取并绘制它。因此,您需要在updateMeasure函数中添加dlmwrite
命令,在{1}}中启动计时器以启动StartMonitor中的计时器,并在StopMonitor中添加tic
和dlmread
。当然这很快且很脏,因为需要删除或检查创建的文件,因此在以后的使用中你不会继续附加它。
这就是一切
plot