matlab doesn't plot the data recieved from serial

3 vues (au cours des 30 derniers jours)
isra
isra le 25 Jan 2017
Commenté : Walter Roberson le 25 Jan 2017
Hello I'm trying to read inputs from an arduino (through the serial port). My readings are (time, ch1, ch2, ch3,ch4) each displayed in a line. Matlab can successfully read them and store them, but I need it to plot them in realtime as well. I've tired this real time plotting code before and it's working, but when I use it with this program, I only ge the blank figure. Here is my code:
clear all;
s = serial('COM6', 'BaudRate', 250000); % setup comport
fopen(s);
t=[]; v1=[]; v2=[];v2=[];v3=[]; v4=[]; % same as in Arduino IDE
n=1;h=0; h2=0; h3=0; h4=0; base=0.0;
servalue= input('Enter the value 1 to start reading :');
fprintf(s,servalue);
%%%%%%%%%%%%%%%%%%setting the plots%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
figure; subplot(411) ;
h = plot(t, v1, 'r-'); % Save handle to the line
title('Channel 1');
ylabel('Amplitude(V)');
subplot(412); h2= plot(t, v2, 'b-'); % Save handle to the line
title('Channel 2');
ylabel('Amplitude(V)');
subplot(413); h3= plot(t, v3, 'b-'); % Save handle to the line
title('Channel 3');
ylabel('Amplitude(V)');
subplot(414); h4= plot(t, v4, 'm-'); % Save handle to the line
title('Channel 4');
xlabel('Time (seconds)');
ylabel('Amplitude(V)');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
tic;
while(toc<10)
% t = vertcat(t , double(fscanf(s, '%f')));
% v1 = vertcat(v1,fscanf(s, '%d')*(5.0 / 1023.0));
% v2 = vertcat(v2, fscanf(s, '%d')*(5.0 / 1023.0));
% v3 = vertcat(v3, fscanf(s, '%d')*(5.0 / 1023.0));
% v4 = vertcat(v4, fscanf(s, '%d')*(5.0 / 1023.0));
t(n) = double(fscanf(s, '%f'));
v1(n) = fscanf(s, '%d')*(5.0 / 1023.0);
v2(n) = fscanf(s, '%d')*(5.0 / 1023.0);
v3(n) = fscanf(s, '%d')*(5.0 / 1023.0);
v4(n) = fscanf(s, '%d')*(5.0 / 1023.0);
%%shifting time to zero
%%since Arduino will save time from when the board was on, not from the
%%beginning of the sensors' reading.
if(n==1)
base = t(n);
end
t(n)= t(n)- base;
%%%%%%%%%updating plots%%%%%%%%%%%%%%%%%%
set(h, 'xdata', t, 'ydata', v1); % Update plot
set(h2, 'xdata', t, 'ydata', v2); % Update plot
set(h3, 'xdata', t, 'ydata', v3); % Update plot
set(h4, 'xdata', t, 'ydata', v4); % Update plot
drawnow;
n=n+1;
end
Besides the blank figure, I sometimes get the following error: In an assignment A(:) = B, the number of elements in A and B must be the same.
This shows up at certain times only, which is weird, in other runs it runs correctly saving the data but not plotting. Can you guide me in this please?
Edit: The results from the workspace after running for 10 secs is in the following picture:
As you can see I got the same size for all the channels except channel 3 (V3). I don't know why is that happening. Also note that the number of iteration (n) matches the size of most of the vectors (except v3)this means I'm reading a value per vector in every iteration, which is what I want, yet I got this warning: Warning: Unsuccessful read: Matching failure in format.... Can anyone point out why is this happening.

Réponses (1)

Walter Roberson
Walter Roberson le 25 Jan 2017
fscanf() by default reads as many values as are present in the input. If the input is empty or if the input contains extra values, then the number of values it reads would not be exactly 1. That would lead to problems in assigning to the single location such as t(n) . You should be telling fscanf to read only 1 value, and you should assign to a temporary variable and check that the result is not empty.
  3 commentaires
isra
isra le 25 Jan 2017
Modifié(e) : isra le 25 Jan 2017
now I tried using the fgetl(obj) to get each line, since I only have one element per line this should work, but again I got the following error: Subscripted assignment dimension mismatch.
Walter Roberson
Walter Roberson le 25 Jan 2017
I read through fscanf.m and did a bunch of testing, and the only circumstances I can figure under which the error about matching format can occur is if no data is read from the serial port under some circumstance that does not trigger an exception in reading from the serial port itself. (I do not have access deep enough into the code to really prove that it can happen this way.)
The error about matching format does not arise if there is input data but it does not match the format you pass in, not even if you pass in a format with invalid syntax: in that case, you would get as much as could be understood (and the rest of the buffer would be dropped.) Serial data that did not match the format could lead to an empty result from fscanf().
Anyhow, I still say that you need to assign the result of the fscanf() to a temporary variable and test whether the result is empty before you try to do the assignment.
Perhaps you might have to loop until you get data.
If you can, sanity-check in case what is happening is that some data is getting lost. Be sure to configure hardware flow control if the remote end supports it.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB Support Package for Arduino Hardware dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by