简体   繁体   中英

How to write data from the accelerometer sensor on a file and write on matlab?

I want to collect data from the Android accelerometer and write it on a file. The file is stored on the SDCard and then I manually copy to the computer by adbpull comand. Then I want to analyze the values on Matlab.

What's the best way to do? I tried writting the parameters as a string but I dont know how to read then on Matlab.

        WriteOnFile(FdataAcc, String.valueOf(event.timestamp) 
                                + " " + mAcceleration[0] 
                                + " " + mAcceleration[1]
                                + " " + mAcceleration[2] + "\n");  

    public void WriteOnFile(File filename, String data){
    try{
        DataOutputStream dos = new DataOutputStream( new FileOutputStream(filename,true));
        //new appended stream
        dos.writeChars(data);
        dos.close();
        }
        catch(Exception e){;}

}

I also tried to write the values as float, but still I cant read on Matlab.

    public void WriteOnFile(File filename, long data){
    try{
        DataOutputStream dos = new DataOutputStream( new FileOutputStream(filename,true));
        dos.writeFloat((float)data);
        dos.writeChars(" ");
        dos.writeFloat((float) mAcceleration[0]);
        dos.writeChars(" ");
        dos.writeFloat((float) mAcceleration[1]);
        dos.writeChars(" ");
        dos.writeFloat((float) mAcceleration[2]);
        dos.writeChars("\n");
        dos.close();
        }
        catch(Exception e){;}

}

What's the best way to do? Should I use Dataoutputstream to write in a file? Sensor values are floats. Thanks in advance.

You should use the second example you showed, but don't write the characters between the floats.

When you do fopen on matlab, make sure you put in the machine format argument. You might need to experiment with it until you get the right format, but it will work. Do help fopen to see the options.

then all you have to do in order to read in all the data is

fid = fopen(filename,'r',MACHINEFORMAT);
data = fread(fid,inf,'float32');  %float32 is for single precision float

Or, if you want to read into an array:

data = fread(fid,[M,inf],'float32');

Where M is the number of elements in each column in the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM