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

· Registered
Joined
·
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?

Code:
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 
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);
Any help is highly appreciated!
 

· Registered
Joined
·
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
Joined
·
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.

Ninjaboi
 

· Registered
Joined
·
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
Joined
·
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
Joined
·
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
Joined
·
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
Joined
·
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
Status
Not open for further replies.
Top