Real-time graph through serial on matlab. PLEASE HELP!!

3 views (last 30 days)
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!

Answers (3)

Walter Roberson
Walter Roberson on 24 Mar 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 Comments
rebecca
rebecca on 25 Mar 2011
Actually the numbers are constantly varying between 0 up to 6 and nothing else. I dont understand what my matlab's reading!
Walter Roberson
Walter Roberson on 25 Mar 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.

Sign in to comment.


rebecca
rebecca on 26 Mar 2011
Ok, i changed that line and now i got the error:
" ??? Error using ==> serial.fread at 131 SIZE must be greater than 0. "
And when i opened that error it displayed:
" error('MATLAB:serial:fread:invalidSIZE', 'SIZE must be greater than 0.'); "
Does this mean that my matlab isnt reading any values? Im a bit worried now because the code in my arduino is good for sure because when i use serial monitor all the values are available to me. So why is my matlab telling me the size is zero? :(
(EDITED) DIFFERENT ERROR: i changed this line
"instring = fread(s,s.BytesAvailable,'uint8');
because for serial ('uint8=>char') doesn't work, and now my new error is
"Error using ==> sscanf First argument must be a string. "
Why isnt matlab readin this line as a string?:
"invalues = sscanf(instring,'%f') "
Isnt "instring" the string?
  2 Comments
Walter Roberson
Walter Roberson on 26 Mar 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
rebecca on 27 Mar 2011
But does this make sense: (without using the line of fread()) When i dont use the debug to stop if a warning occurs, my code runs and just displays lines and lines of something like this:
columns 1000 through 1005
3 3 3 3 3 3 3 3 3 3 3 3 3 3
where the numbers next to "columns" and "through" are increasing every 0.2s (as set to my code) and the numbers below it vary between 0 and 6 (depending how i position my device). Does it mean that it's working or? (If so i dont know why its not displayed in a graph!)
But why is it that when i switch on the debug to stop if a warning occurs, the code stops running and gives me a warning saying "Warning: Matching failure in format"? in the line of fscanf()?

Sign in to comment.


farrukh sabir
farrukh sabir on 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/

Categories

Find more on MATLAB Support Package for Arduino Hardware 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!