AHRS on Matlab lengths error
1 view (last 30 days)
Show older comments
Hello to everyone,
A part of my thesis is to build an AHRS in matlab but when I'm trying to plot some data, from a gyroscope connected to arduino, in matlab, i'm getting the following error
??? Error using ==> plot Vectors must be the same lengths.
Error in ==> serialread at 14
plot(counterz,y(data));
Here is my code
function serialread(zaxis)
y=zeros(1,1000);
s=serial('COM1');
fopen(s);
counterz=1;
while counterz<=zaxis
ylim([-150 150]);
data=fscanf(s,'%s');
%y(data)=(data)*5/1024;
plot(counterz,y(data));
drawnow
counterz=counterz+1;
end
fclose(s);
delete(s);
clear all;
end
What am i doing wrong?
the code on arduino is this
#include <Wire.h>
#include <L3G.h>
L3G gyro;
void setup() {
Serial.begin(9600);
Wire.begin();
if (!gyro.init())
{
Serial.println("Failed to autodetect gyro type!");
while (1);
}
gyro.enableDefault();
}
void loop() {
gyro.read();
Serial.println((int)gyro.g.z);
delay(100);
}
Thanks alot in advance,
Minas
0 Comments
Answers (3)
Walter Roberson
on 15 Feb 2013
The fscanf function reapplies the format throughout the entire file, and positions the file pointer at the end-of-file marker.
So you are reading in all of the strings in the file and assigning that to a variable, where it will become a row vector of characters.
Then you are plot(counterz, y(data)). You index an array y() at the row vector of characters, getting one result per character. But counterz is a scalar.
You probably want to read with a numeric format such as '%d' or '%f' rather than with a string format '%s'. And you probably only want to read one value at a time; consult the fscanf() documentation for how to do that. Lastly, you are probably going to want to have a "hold on" unless you just want to display one point, erasing all the others when plot() is called again.
0 Comments
See Also
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!