Why is my matlab code getting slower and slower?
显示 更早的评论
Hi, I use Matlab to read data from a digital multimeter through serial communication and plot the real-time data. The time interval is set to 1s. In the beginning, the code works well. However, as the time passes, the code gets slower and slower, and the time interval becomes 2s, 3s.... Could anyone help me? my code is as follows:
%%Initial
clear all
clc
%%Create the serial object
serialPort = 'COM5';
serialObject = serial(serialPort);
set(serialObject,'BaudRate',9600,'DataBits',8,'StopBits',1,'Terminator','CR');
fopen(serialObject);
time = now;
Channum = 2;
left = 0.15;
bottom = 1/Channum;
length = 0.7;
height = 1/Channum;
%%Set the time span and interval for data collection
% stopTime = '06/30 17:41';
timeInterval = 1;
%%Collect data
count = 1;
hour = 12;
Timespan = 3600*hour;
% Timespan = 20;
voltage = zeros(Timespan,Channum);
% DMM set
fprintf(serialObject,'SYSTEM:REMOTE');
fprintf(serialObject,'*RST'); % reset
fprintf(serialObject,':SENSE:FUNC "VOLT" '); % set voltage mesurement mode
fprintf(serialObject,'Voltage:DC:Range:Upper 10'); % set range
fprintf(serialObject,'Voltage:DC:nplc 0.01');% set nplc to 0.01
fprintf(serialObject,'route:scan:internal (@1,2)');% scan channel 1:3
fprintf(serialObject,'route:scan:lselect internal'); % set to internal scanning
fprintf(serialObject,':Sample:Count 2'); % Set number of scan channels
fprintf(serialObject,':Trace:Points 2'); % Set buffer size
for count = 1:Timespan
tic
time(count) = datenum(clock); % Get present time
% Get voltage data
fprintf(serialObject,':Trace:Feed:Control Next'); % Set trace mode
fprintf(serialObject,'Init'); % Init a measure
fprintf(serialObject,':Trace:Data?'); % read buffer data
Temp{count} = fscanf(serialObject,'%c');
V_num = str2num(Temp{count});
voltage(count,1) = V_num(1);
voltage(count,2) = V_num(2);
clear V_num
% Plot data
for i = 1:Channum
h(i) = subplot(ceil(Channum/2),2,i);
plot(time(count),voltage(count,i),'Marker','.','LineWidth',1,'Color','b');
datetick('x','mm/DD HH:MM');
hold on
end
disp(count)
timecost = toc
pause(timeInterval-timecost);
end
%%Put the instrument in local mode
fprintf(serialObject,'*RST');
fprintf(serialObject,'SYSTEM:LOCAL');
%%Clean up the serial object
fclose(serialObject);
delete(serialObject);
clear serialObject;
采纳的回答
更多回答(1 个)
Walter Roberson
2017-7-17
0 个投票
You are not preallocatting the arrays you store the results into. Each iteration that you add something beyond the existing end of the array, it has to create a new larger array and copy all of the old content over and then delete the smaller array. The accumulated costs of doing that rise with the square of the number of iterations.
It would be more efficient if you could define a maximum number of values to read and allocate the arrays that large in the beginning.
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!