Growing array with fscanf data in matlab
1 view (last 30 days)
Show older comments
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 Comments
dpb
on 5 Aug 2016
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...
Answers (1)
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!