Growing array with fscanf data in matlab

1 visualización (últimos 30 días)
Ethan Gardner
Ethan Gardner el 4 de Ag. de 2016
Comentada: dpb el 5 de Ag. de 2016
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 comentarios
Ethan Gardner
Ethan Gardner el 5 de Ag. de 2016
I actually tested that out awhile back and found that it was a problem with the plot i still need to be able to plot but i need to do it in a more efficent manner. the code i have for the plot now looks like this:
if true
% code
clear all;
max = 80;
min = 40;
amp = (max-min)/2;
offset = amp + min;
btime = 2;
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;
figure(1);
hold on;
title('Incomming Data from External Device');
xlabel('Data Number');
ylabel('Analog Voltage (0-1023)');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% while loop & Timing Data
i = 1;
y = zeros(length(x),1);
t2 = tic;
while i<=length(x)
t = tic;
disp(['<P' num2str(x1(i)) '>']);
y = sin(i);
r = rem(i,100);
figure(1);
if i<=100
xlim([0 i]);
ylim([-1 1]);
d = (1:i);
else
xlim([i-100 i]);
ylim([-1 1]);
d = (i-100:i);
end
% if r == 0
% clf(figure(1),'reset');
% end
plot(i,y,'b*');
drawnow;
while toc(t) < 0.05
continue
end
i = i+1;
end
t3 = toc(t2);
disp(t3);
%t3 = 166 seconds expect ~120
end
so i am now trying to figure out how to make this still plot in real time but still be fast i only need x amount of data points to be shown at one time. this runs faster but still not optimally as if i was just plotting one point on the graph is it possible to speed it up.
dpb
dpb el 5 de Ag. de 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...

Iniciar sesión para comentar.

Respuestas (1)

Ethan Gardner
Ethan Gardner el 5 de Ag. de 2016
figured it out this works perfectly for my applicaiton
if true
clear all;
max = 80;
min = 40;
amp = (max-min)/2;
offset = amp + min;
btime = 2;
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;
figure(1);
hold on;
title('Incomming Data from External Device');
xlabel('Data Number');
ylabel('Analog Voltage (0-1023)');
figure(2);
hold on;
title('Incomming Data from External Device');
xlabel('Data Number');
ylabel('Analog Voltage (0-1023)');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% while loop & Timing Data
i = 1;
y = zeros(length(x),1);
t2 = tic;
while i<=length(x)
t = tic;
disp(['<P' num2str(x1(i)) '>']);
y(i) = sin(i);
r = rem(i,100);
figure(1);
if i<=100
xlim([0 i]);
%ylim([-1 1]);
d = (1:i);
else
xlim([i-100 i]);
%ylim([-1 1]);
d = (i-100:i);
end
if isinteger(int64(r)) == true
clf(figure(1),'reset');
end
plot(d,y(d),'b*');
drawnow;
while toc(t) < 0.05
continue
end
% figure(2);
% plot(i,y(i),'b*');
i = i+1;
end
t3 = toc(t2);
disp(t3);
% d =1:length(x);
% figure(1);
% plot(x,y,'b*');
%t3 = 166 seconds expect ~120
end

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by