2017-03-24 1 views
1

Ich habe einen Code, der die CPU-Auslastung von Matlab zeigt, mein Problem ist, wie die CPU-Auslastung im Laufe der Zeit PLOT, da der Code keine Variablen im Arbeitsbereich speichert, brauche ich ein Methode zum Speichern der CPU-Auslastung in einem Array, wenn der Code ausgeführt wird, so dass ich es plotten kann Vielen Dank für Ihre Zeit. hier ist der Code:Wie die CPU-Auslastung von Matlab plotten

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 
+1

Plotten es wie es läuft oder wenn es fertig ist? – excaza

+0

Ich muss es plotten, wenn es fertig ist –

+0

definieren "fertig", wenn Sie auf Stopp drücken? wenn Sie das Programm beenden? – bla

Antwort

1

ein Weg ist, die Variablen in einer Datei zu speichern und lesen und zeichnen es, nachdem Sie stoppen. So fügen Sie einen dlmwrite Befehl in der Funktion updateMeasure, eine tic zum Starten des Timers in StartMonitor und dlmread und plot in StopMonitor hinzu. Natürlich ist dies schnell und schmutzig, da die erstellte Datei gelöscht oder überprüft werden muss, damit Sie sie später nicht mehr anhängen.

Das ist alles

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; 
dlmwrite('cpulog.txt', [toc, cpu.total , cpu.matlab], '-append'); 

    %// 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) 
tic 
end 

function StopMonitor(obj,evt) 
h = guidata(obj); 
stop(h.t) 
data=dlmread('cpulog.txt'); 
data(1,:)=[]; 
figure;plot(data(:,1),data(:,2),data(:,1) ,data(:,3)); 
legend('total cpu','matlab cpu'); xlabel('sec'); ylabel('%'); 
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

Dieser Code funktioniert perfekt. DANKE SO VIEL –

+1

besten Dank ist die Antwort zu akzeptieren ... :-) – bla