i want to read the serial data using for loop... and want to get a real time data plot
조회 수: 5 (최근 30일)
이전 댓글 표시
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
Jan
2011년 2월 26일
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.
답변 (5개)
Jan
2011년 2월 26일
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
Jan
2011년 2월 27일
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
2011년 2월 26일
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
vishwash saxena
2011년 2월 26일
댓글 수: 1
Paulo Silva
2011년 2월 26일
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))
Ankit Desai
2011년 4월 15일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!