AHRS on Matlab lengths error

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

Respuestas (3)

Walter Roberson
Walter Roberson el 15 de Feb. de 2013

0 votos

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.
Minas
Minas el 20 de Feb. de 2013

0 votos

Thank you alot for you help. It cleared some missunderstanding i had in the fscaf command. So here comes my second question.
I want to read serially from arduino this sequence of numbers -242 42 154 which with a delay of .5 second refreshes and i get the next and the next. I want to read it on matlab so i have to use fscanf.
There are 3 different numbers with a range of -300 to 300 lets say and i seperate these 3 numbers with spaces. How can i read them with fscanf? Im trying this fscanf('%d %d %d') is that correct?
Than you again for your help
Minas
Minas el 21 de Feb. de 2013

0 votos

Ah nice!
I'v reached something here. I manage to get one dot inside my plot with the following error
A timeout occurred before the Terminator was reached or SIZE values were available.
Thank you alot for the tip. that solved the previous error.
What i want to do is to plot for 500 fractions of time the data i recieve. so i'm using this code;
function serialread(zaxis)
y=zeros(1,1000);
s=serial('COM3');
fopen(s);
counterz=1;
while counterz<=zaxis
ylim([-150 150]);
data=fscanf(s, '%d %d %d', 1);
plot(counterz,data);
drawnow
counterz=counterz+1;
end
fclose(s);
delete(s);
clear all;
end
On matlab's conmmand line i just write seriealread(500) But every like 10 seconds matlab freezes and after it i get a plot with a spot in the middle and the y axis increased by one.
Any suggestions on that one?
Thank you alot for your help
Minas

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Centro de ayuda y File Exchange.

Preguntada:

el 14 de Feb. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by