Real-time graph through serial on matlab. PLEASE HELP!!
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hey guys,
Im making use of this code but i keep getting the error: " ??? In an assignment A(I) = B, the number of elements in B and I must be the same. " Can you tell me which part of my code needs to be changed please?
SerialPort='com6'; %serial port
MaxDeviation = 3;%Maximum Allowable Change from one value to next
TimeInterval=0.2;%time interval between each input.
loop=120;%count values
%%Set up the serial port object
s = serial(SerialPort)
fopen(s);
time =now;
voltage = 0;
%%Set up the figure
figureHandle = figure('NumberTitle','off',...
'Name','Voltage Characteristics',...
'Color',[0 0 0],'Visible','off');
% Set axes
axesHandle = axes('Parent',figureHandle,...
'YGrid','on',...
'YColor',[0.9725 0.9725 0.9725],...
'XGrid','on',...
'XColor',[0.9725 0.9725 0.9725],...
'Color',[0 0 0]);
hold on;
plotHandle = plot(axesHandle,time,voltage,'Marker','.','LineWidth',1,'Color',[0 1 0]);
xlim(axesHandle,[min(time) max(time+0.001)]);
% Create xlabel
xlabel('Time','FontWeight','bold','FontSize',14,'Color',[1 1 0]);
% Create ylabel
ylabel('Voltage in V','FontWeight','bold','FontSize',14,'Color',[1 1 0]);
% Create title
title('Real Time Data','FontSize',15,'Color',[1 1 0]);
%%Initializing variables
voltage(1)=0;
time(1)=0;
count = 2;
k=1;
while ~isequal(count,loop)
%%Re creating Serial port before timeout
k=k+1;
if k==25
fclose(s);
delete(s);
clear s;
s = serial('com6');
fopen(s)
k=0;
end
%%Serial data accessing
voltage(count) = fscanf(s,'%f');
%%For reducing Error Use your own costant
voltage(1)=0;
if ((voltage(count)-voltage(count-1))>MaxDeviation)
voltage(count)=voltage(count-1);
end
time(count) = count;
set(plotHandle,'YData',voltage,'XData',time);
set(figureHandle,'Visible','on');
datetick('x','mm/DD HH:MM');
pause(TimeInterval);
count = count +1;
end
Any help is highly appreciated!
Antworten (3)
Walter Roberson
am 24 Mär. 2011
You did not indicate which line you are encountering the error on.
In the line,
voltage(count) = fscanf(s,'%f');
If there is more than one number available in the buffer, then fscanf() would return multiple values, which you then try to store in a single array location.
7 Kommentare
Walter Roberson
am 25 Mär. 2011
Which variable is being displayed at that time? And could you show us a line of the output?
At the place I suggested
invalues = fscanf(s,'%f');
I suggest you change that to
instring = fread(s,s.BytesAvailable,'uint8=>char');
invalues = sscanf(instring, '%f');
and put in the breakpoint like described above. When it stops at the breakpoint, display the instring variable: it will show you the text that was available to be parsed.
rebecca
am 26 Mär. 2011
2 Kommentare
Walter Roberson
am 26 Mär. 2011
I forgot that you are using serial. It defaults to reading a byte at a time in character mode, so you can use
instring = fread(s,s.BytesAvailable);
If you encounter errors about the size being 0, it means that there are no bytes available to read right then (but there might be later.)
Warning: using fread() like this will grab _all_ of the available bytes. If it is in the middle of receiving a "number", then that number might end up getting split between one fread() call and the next. Using fread() like this is for debugging purposes: once you figure out what data you are receiving, you can adjust the fscanf() calls without using fread().
farrukh sabir
am 21 Sep. 2013
it is not problem with matlab code, it is problem on sending side, I faced similar problem but then I fixed the way I was sending data from microcontroller to matlab, I wrote this code in for a PIC MCU which works perfect
temp=ADC_Get_Sample(0);
inttostr(temp,str);
uart1_write_Text(str);
uart1_write(0x0d);
delay_ms(1000);
Farrukh join my group https://www.facebook.com/groups/picMicrocontrollers/
0 Kommentare
Siehe auch
Kategorien
Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!