繁体   English   中英

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

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

我想从Android加速度计收集数据并将其写入文件中。 该文件存储在SDCard上,然后由adbpull comand手动将其复制到计算机。 然后,我想分析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){;}

}

我也尝试将值写为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){;}

}

最好的方法是什么? 我应该使用Dataoutputstream写入文件吗? 传感器值为浮点型。 提前致谢。

您应该使用显示的第二个示例,但不要在浮点数之间写字符。

在matlab上执行fopen时,请确保输入了机器格式参数。 您可能需要尝试一下它,直到获得正确的格式,但它仍然有效。 help fopen以查看选项。

那么您要做的就是读取所有数据

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

或者,如果您想读入数组:

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

其中M是数组中每一列中的元素数。

暂无
暂无

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

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