Tech Support Forum banner
Not open for further replies.
1 - 10 of 10 Posts

· Registered
5 Posts
Discussion Starter · #1 ·
Hey guys,

Ive got an arduino plugged into my laptop and connected to my matlab so that i can display 6 outputs coming from my arduino (serial through 1 port)onto a matlab graph.
Im making use of this code but i keep getting the error: "Warning: Matching failure in format." What does this mean?
When i enter the full code my matlab remains on "busy" and my arduino remains on "transmitting" but nothing happens (i dont even see a graph!).

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)
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,...
    'YColor',[0.9725 0.9725 0.9725],...
    '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
count = 2;
while ~isequal(count,loop)
%%Re creating Serial port before timeout      
      if k==25
  clear s;        
  s = serial('com6');
%%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) 
time(count) = count; 
count = count + 1; 
Any help is highly appreciated!

· Registered
5 Posts
Discussion Starter · #3 ·
I dont think that's my problem as i use 'fscanf' not 'sscanf' because i dont have strings. Or are u suggesting i turn my outputs to strings? If so can u show me an example please?
I also tried invalues = fscanf(s %f); instead but matlab decalared that an error due to lack of punctuation.

· TSF-Emeritus
2,063 Posts
Honestly I've barely even looked at MATLAB code, and have never programmed in the language personally. I just noticed that and made a partial assumption.

Other than what I've already provided, you could try going here. It's a link that provides both information and examples using/involving sscanf.

Sorry if neither of my post help.


· Registered
5 Posts
Discussion Starter · #5 ·
Hrmm, i followed your link. It helped a bit as i used it for a guidline to change my "invalues" to a string. I added this:
instring = fread(s,s.BytesAvailable,'uint8=>char');
invalues = sscanf(instring, '%f');

But now im getting a different error telling me:
error('MATLAB:serial:fread:invalidSIZE', 'SIZE must be greater than 0.');
Is it not recieving my bytes from my arduino in other words? :S

· TSF-Emeritus
2,063 Posts
Read binary data from device - MATLAB

The link above is information on fread().

A = fread(obj,size,'precision')
That is mentioned in the link I provided. From what you've provided:

instring = fread(s,s.BytesAvailable,'uint8=>char');
s = Your object.
s.BytesAvailable = Your size available.
'uint8=>char' = Your precision.

Seeing is how your problem is the size parameter, I'd point your attention to 'BytesAvailable'.

Number of bytes available in the input buffer - MATLAB

The link above provides information to 'bytesavailable'.

You can make use of BytesAvailable only when reading data asynchronously. This is because when reading data synchronously, control is returned to the MATLAB command line only after the input buffer is empty. Therefore, the BytesAvailable value is always 0.
Seeing is how your error says size must be greater than 0, further reading of the link I provided would help you on this matter. If not, say so and we'll go from there.

· Registered
5 Posts
Discussion Starter · #7 ·
I read through the links you provided. To be honest i had used the "fread()" in my code to make my errors more visible to me, because in actual fact i dont need that line.
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
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()?

· TSF-Emeritus
2,063 Posts
Lol again I'm not a MATLAB programmer, I just did a quick observation on your code and errors to make an educated guess on the solution. If anyone else is able to help you here at TSF, they will come. I'll see if anyone else has ideas that will post here.

Also, I did some browsing and found that you have another thread made in another forum for this exact problem:

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

Try giving them the information you've provided of recent here, that way they can help you better.

· Registered
5 Posts
Discussion Starter · #9 ·
Oh right!! Im sorry, i totally forgot u dont work on matlab! haha, my bad. Yea, ive asked on other forums but it seems rly hard just to get some help from people sometimes :(
Thanks so much for trying to help though, i really appreciate it. I just hope i'll be able to move past this :/ Thanks again ninjaboi!
1 - 10 of 10 Posts
Not open for further replies.