Real-time graph through serial on matlab. PLEASE HELP!!
Mostrar comentarios más antiguos
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!
1 comentario
rebecca
el 25 de Mzo. de 2011
Respuestas (3)
Walter Roberson
el 24 de Mzo. de 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 comentarios
rebecca
el 24 de Mzo. de 2011
Walter Roberson
el 24 de Mzo. de 2011
%%Serial data accessing
invalues = fscanf(s,'%f');
for K = 1 : length(invalues)
voltage(count) = invalues(K);
if voltage(count)-voltage(count-1) > MaxDeviation
voltage(count) = voltage(count-1)
end
time(count) = count;
count = count + 1;
end
set(plotHandle,'YData',voltage,'XData',time);
pause(TimeInterval);
But do *not* have the count = count +1; statement after the pause()
rebecca
el 24 de Mzo. de 2011
Walter Roberson
el 25 de Mzo. de 2011
Matching failure in format means that it encountered something that could not be interpreted as a floating point number. The data that could not be processed would be left in the buffer, so the next time you tried to read with %f it would still be there and would generate the error again.
I suggest that at the command window, you command
dbstop if warning
and then run, and then when the program stops in the debugger, use fgetl(s) to see what is in the buffer until the end of the line.
rebecca
el 25 de Mzo. de 2011
rebecca
el 25 de Mzo. de 2011
Walter Roberson
el 25 de Mzo. de 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
el 26 de Mzo. de 2011
0 votos
2 comentarios
Walter Roberson
el 26 de Mzo. de 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().
rebecca
el 27 de Mzo. de 2011
farrukh sabir
el 21 de Sept. de 2013
0 votos
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/
Categorías
Más información sobre MATLAB Support Package for Arduino Hardware en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!