Growing array with fscanf data in matlab
古いコメントを表示
I am trying to send a value to an arduino from Matlab and read serial data with fscanf() in matlab from the arduino.
My requirements are that i need to be able to send the data at a specified time interval i.e 20 values persecond at .05 seconds. and i need to be able to graph the incoming data and be able to save it for later use. I will know the length of the array before I start I have read and seen that this can be used to speed up the process.
The problem i am currently having is that each iteration of the loop becomes successively longer which throws the time behavior off more. I believe this to be due to the growing array inside of the loop I have.
delete(instrfindall);
clear all; clc; close all;
oldserial1 = instrfind ('Port', 'COM8');
if (~isempty(oldserial1))
disp('COM8 is in use; deleting now');
delete(oldserial1)
end
handles.UltraM=serial('COM8', 'BaudRate', 115200,'Timeout',10000000);
handles.UltraM.InputBufferSize= 10^6;
handles.UltraM.OutputBufferSize= 10^6;
fopen(handles.UltraM);
max = 80;
min = 40;
amp = (max-min)/2;
offset = amp + min;
btime = 5;
bpm = 12;
spb = 60/bpm;
sapb = spb/.05;
tosd = sapb*bpm*btime;
time1 = btime*60;
x = linspace(0,time1,tosd)';
x1 = amp*sin(x*(2*pi/20)) + offset;
y = zeros(length(x),1);
i = 1;
figure(1);
hold on;
title('Pressure Data');
xlabel('Data Number');
ylabel('Analog Voltage (0-1023)');
t1 = zeros(length(x),1);
figure(2);
hold on;
title('Time to execute task');
xlabel('iteration number');
ylabel('time taken');
while (i<=length(x))
t2 = tic;
t = tic;
fprintf(handles.UltraM,(['<P' num2str(x1(i)) '>']));
disp((['<P' num2str(x1(i)) '>']));
y(i) = fscanf(handles.UltraM,'%i');
figure(1);
hold on;
plot(i, y(i), 'b*');
plot(i, pressure, 'b*');
drawnow;
hold off;
while toc(t) < 0.05
continue
end
t1 = toc(t2);
figure(2);
hold on;
plot(i,t1,'b*');
drawnow;
hold off;
i = i + 1;
end
The last tic toc finction is just used so that i can graphically see the slowdown time.
thank you in advance for any help
3 件のコメント
dpb
2016 年 8 月 4 日
"...each iteration of the loop becomes successively longer ...[and]... I believe this to be due to the growing array inside of the loop"
x = linspace(0,time1,tosd)';
...
y = zeros(length(x),1);
...
while (i<=length(x))
t2 = tic;
t = tic;
fprintf(handles.UltraM,(['<P' num2str(x1(i)) '>']));
disp((['<P' num2str(x1(i)) '>']));
y(i) = fscanf(handles.UltraM,'%i');
...
You have preallocated y; I don't see which array you think is "growing" inside the loop?
I wonder if the slowdown isn't more due to adding more datapoints to the plot instead...see what happens if you just comment out all the stuff updating the plot and only read the data just to confirm/deny and determine what is (or, just as importantly, isn't) the bottleneck. Gotta' be sure you're attacking the right problem, first.
Ethan Gardner
2016 年 8 月 5 日
dpb
2016 年 8 月 5 日
See the section in the doc on "Animation" under the 2D/3D Plots subject for detailed discussion; for this type the use of simply redefining the [X|YData] properties and updating appropriately instead of using the high-level calls...
回答 (1 件)
Ethan Gardner
2016 年 8 月 5 日
カテゴリ
ヘルプ センター および File Exchange で Graphics Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!