简体   繁体   English

如何将来自加速度传感器的数据写入文件并在matlab上写入?

[英]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. 我想从Android加速度计收集数据并将其写入文件中。 The file is stored on the SDCard and then I manually copy to the computer by adbpull comand. 该文件存储在SDCard上,然后由adbpull comand手动将其复制到计算机。 Then I want to analyze the values on Matlab. 然后,我想分析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. 我试图将参数写为字符串,但是我不知道如何在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. 我也尝试将值写为float,但仍然无法在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? 我应该使用Dataoutputstream写入文件吗? 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. 在matlab上执行fopen时,请确保输入了机器格式参数。 You might need to experiment with it until you get the right format, but it will work. 您可能需要尝试一下它,直到获得正确的格式,但它仍然有效。 Do help fopen to see the options. help fopen以查看选项。

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. 其中M是数组中每一列中的元素数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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