MATLAB errors with Arduino

I am trying to generate a line plot in real time in MATLAB using data from an Arduino. The data is from an MPU6050, from the accelerometer. I used the MATLAB code from this link with my own Arduino code

Arduino Code

// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class
// 10/7/2011 by Jeff Rowberg <jeff@rowberg.net>
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
//      2013-05-08 - added multiple output formats
//                 - added seamless Fastwire support
//      2011-10-07 - initial release

/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jeff Rowberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/


#include "I2Cdev.h"
#include "MPU6050.h"

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
    #include "Wire.h"
#endif

MPU6050 accelgyro;


int16_t ax, ay, az;
int16_t gx, gy, gz;

#define OUTPUT_READABLE_ACCELGYRO

#define LED_PIN 13
bool blinkState = false;

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif

    // initialize serial communication
    // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
    // it's really up to you depending on your project)
    Serial.begin(9600);

    // initialize device
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

    // configure Arduino LED for
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

    // these methods (and a few others) are also available
    //accelgyro.getAcceleration(&ax, &ay, &az);
    //accelgyro.getRotation(&gx, &gy, &gz);

    #ifdef OUTPUT_READABLE_ACCELGYRO
        // display tab-separated accel/gyro x/y/z values
        Serial.print("ACCEL(x,y,z)\t");
        Serial.print(ax); Serial.print("\t");
        Serial.print(ay); Serial.print("\t");
        Serial.print(az); Serial.print("\t");
        Serial.print("GYRO(x,y,z)\t");
        Serial.print(gx); Serial.print("\t");
        Serial.print(gy); Serial.print("\t");
        Serial.println(gz);
        delay(333);
    #endif

    #ifdef OUTPUT_BINARY_ACCELGYRO
        Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
        Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
        Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
        Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
        Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
        Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
        delay(333);
    #endif

    // blink LED to indicate activity
   // blinkState = !blinkState;
   // digitalWrite(LED_PIN, blinkState);
}

For MATLAB

clear all;
close all;

com=serial('COM17','Baudrate',9600);
fopen(com);

n=1000;

acx=zeros(1,n);
acy=acx;
acz=acx;

f=figure(1);

for i=1:n
    ax=fscanf(com,'%d');
    ay=fscanf(com,'%d');
    az=fscanf(com,'%d');
    axis([i-40 i+40 -4.8 14.8]);
    
    if i>1
        acx(i)=ax/1670.13; %convert to m/s^2
        acy(i)=ay/1670.13; %convert to m/s^2
        acz(i)=az/1670.13; %convert to m/s^2
        a(i)=line([i-1,i],[acx(i-1),acx(i)]);
        b(i)=line([i-1,i],[acy(i-1),acy(i)],'color','r');
        c(i)=line([i-1,i],[acz(i-1),acz(i)],'color','m');
        drawnow
    end   
    if i>40
        delete(a(i-40))
        delete(b(i-40))
        delete(c(i-40))
    end
    if ~ishghandle(f) 
        break             % break out of loop when figure(1) is closed
    end
end
fclose(com);
delete(com);
delete(instrfindall);

However, when I run it, I run into these errors:

  • Warning: MATLAB has disabled some advanced graphics rendering features by switching to software OpenGL. For more
    information, click here.
    Warning: Unsuccessful read: Matching failure in format..
    Warning: Unsuccessful read: Matching failure in format..
    Warning: Unsuccessful read: Matching failure in format..
    In an assignment A(:slight_smile: = B, the number of elements in A and B must be the same.

Error in TestCodeForAccelerometer3 (line 22)
acx(i)=ax/1670.13; %convert to m/s^2

If I delete this section

        acx(i)=ax/1670.13; %convert to m/s^2
        acy(i)=ay/1670.13; %convert to m/s^2
        acz(i)=az/1670.13; %convert to m/s^2

I end up getting a real time plot that still has the error

  • Warning: Unsuccessful read: Matching failure in format..

And the real time plot only shows one plot with a straight line having a y value of 0 increasing in x value

If anyone could help me
Thanks

You want us to help you debug Matlab code? THINK again.