i want to read the serial data using for loop... and want to get a real time data plot
3 views (last 30 days)
Show older comments
function plot_Callback(hObject, eventdata, handles)
a1=0;
for i=1:1000
obj = serial('COM1');
set(obj,'BaudRate',9600)
fopen(obj); %open port
a = fscanf(obj); % read data into variable a
plot(a);
drawnow;
pause(0.5)
end
title('Axes 1');
xlabel('X data');
ylabel('Y data');
fclose(obj)
delete(obj);
clear obj;
will it work????... and do i need to create the object and set it for each run of for loop... and can u pls tell me what is the difference between com1 and com2?
1 Comment
Jan
on 26 Feb 2011
Please cleanup your code. "a1" is not used. Omit everything, which does not cause troubles, e.g. title and labels. The readers will spend more time im helping you, if the loose less time in interpreting your code. Therefore a standard code formatting is recommended also: Start with two spaces, leave an empty line before the code.
Answers (5)
Jan
on 26 Feb 2011
COM1 is the first serial interface of your computer, COM2 is the second. Guess what COM3 is.
It is not clear , what you want to achieve. With some wild guessing I assume it will not work.
- Open the serial object once only before the loop.
- Does FSCANF work with serial objects in the shown way?
- Perhaps "plot(a)" would work, but it is not clear, if it does, what you want. Please describe any details.
obj = serial('COM1');
set(obj,'BaudRate',9600)
fopen(obj); %open port
for i=1:1000
a = fscanf(obj); % ??? Does this work?
plot(a); % ??? Display the value of a at first
pause(0.5); % Calls DRAWNOW implicitely
end
3 Comments
Jan
on 27 Feb 2011
I asked you to cleanup the posted code as comment to your question. I asked three questions here in this answer. I cannot see a reaction from your side. Therefore I assume the problem is not urgent. Good luck.
Paulo Silva
on 26 Feb 2011
Here's a code that I made that plots the memory used by matlab in real time, I used some tips from the experts ;) adapt it to your needs.
function test
fig=figure;[user,sys] = memory;a=0;
set(fig,'CloseRequestFcn','delete(gcf);')
ax=axes;set(ax,'Ylimmode','manual');
h = plot(ax,NaN, NaN,'LineWidth',5);
set(ax,'Ylimmode','manual');
set(ax,'YLim',[0 sys.PhysicalMemory.Total/10^6])
ylabel('Memory used by matlab in Megabytes');
t = timer('TimerFcn',@readdata,...
'Period', 0.5,'executionmode','fixedSpacing');start(t)
function readdata(g1,gg1)
if (~ishandle(fig)),stop(t),return,end
m=memory;mem=m.MemUsedMATLAB;
set(h,'XData', [get(h, 'XData'), a],...
'YData', [get(h, 'YData'), mem/10^6]);
xd=get(h,'XData');yd=get(h,'YData');a=a+1;
if numel(xd)>=100
set(h,'XData',[xd(80:100)]);set(h,'YData',[yd(80:100)]);
end
end
end
0 Comments
vishwash saxena
on 26 Feb 2011
1 Comment
Paulo Silva
on 26 Feb 2011
Here's one example that you can use and adapt, it reads the value of the memory used by matlab each second for 100 seconds and plots it, instead of your time values you can use just a number composed of hour minute second for each reading.
CData=[];
for a=1:100
c = clock; %[year month day hour minute seconds]
m=memory;
CData=[CData;m.MemUsedMATLAB/10^6 str2num(sprintf('%d%d%d',c(4),c(5),c(6)))]
pause(1)
end
%In the end you can plot the data
plot(CData(:,2),CData(:,1))
vishwash saxena
on 27 Feb 2011
1 Comment
RITESH KUMAR
on 12 Sep 2019
How can i get continuous x data in excelsheet(csv file) in this program.
Ankit Desai
on 15 Apr 2011
You might also want to consider using an event based read from serial port rather than a while or for loop.
If you are getting a datastream on the serial port you might want to take a look at the example I put on file exchange that shows event based read using a TCPIP object and plotting the read data. You can change the code to use a serial object.
If you are getting data after a query you might want to take a look at this example on file exchange that shows how to get data & plot using query based interactions.
Hope this helps,
-Ankit
0 Comments
See Also
Categories
Find more on Data Import and Analysis 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!