Read and send data every second and plot against time
15 views (last 30 days)
Show older comments
Hi,
I got a simple task but I just can't figure out how to solve it. I got a program that reads data from an instrument every 10 seconds. Furthmore it sends out data every second to another instrument. This read in routine is embedded in a while loop and the send out routine is in a run loop. Too keep it simple I'd like to plot the data that is send out over time.
As an example: I want Matlab to open a figure where it starts plotting the data with time on the x-axis. What I need for that would be some sort of continuously running counter. I tried datetick but it doesn't really work. It only shows 00 everywhere.
Not sure if I can simplify the code enough for you to understand but here is the important part:
while x == 1
t1 = timer('TimerFcn',' ','StartDelay',10); %wait 10sec until next measurement
t2 = timer('TimerFcn',' ','StartDelay',1);
start(t1)
wait(t1)
%here would come some code for reading the data
%not important right now
for i=1:10
%some code for sending out data: data_out
plot(data_out)
start(t2)
wait(t2)
end
end
When I now plot that data_out it only works for the duration of the run loop (in this case for 1 - 10 seconds). Then 10 seconds later the run loop is executed again and the plot starts again with x=1 instead of x=11.
I bet there is an easy way to do this but as I said nothing I tried so far worked.
I appreciate every help. Thanks a lot.
0 Comments
Answers (2)
Walter Roberson
on 29 Nov 2011
Before the loop:
xoffset = 0;
In the loop:
numout = length(data_out);
plot(xoffset+(1:numout), data_out);
xoffset = xoffset + numout;
2 Comments
Balu
on 1 Feb 2015
s1 = serial('COM4','BaudRate',9600,'DataBits',8);
data=[]; x=0; tic while toc<20
fopen(s1) a=fread(s1,100) fclose(s1);
data=[data;a]; if(length(data)<501) plot(data); else x=x+100; plot(data(0+x:500+x)) % x=x+100; end pause(0.5); % hold on;
end fclose(s1);
0 Comments
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!