如何记录传感器数据并导出为CSV? [英] How to log sensor data and export to CSV?

查看:52
本文介绍了如何记录传感器数据并导出为CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS应用程序,它使用CoreMotion以约5​​0Hz的频率将传感器数据(陀螺仪,姿态,加速度等)打印到终端.我想保存传感器读数并将其导出到csv文件.我将使用哪些框架(如果有)来执行此操作?我一直在尝试使用Core Data,但我觉得它不是正确的工具.

I have an iOS app that prints sensor data (gyroscope, attitude, acceleration, etc.) to the terminal at ~50Hz with CoreMotion. I want to save the sensor readings and export them to a csv file. Which frameworks, if any, would I use to do this? I've been trying to use Core Data but I feel like it's not the correct tool to use.

作为参考,这是我读取加速度计数据的方式:

for reference, this is how I read accelerometer data:

func startAccel() ->Void{ //Starting accelerometer
    motionManager.accelerometerUpdateInterval = 1.0 / Double(hz)
    motionManager.startAccelerometerUpdates(to: OperationQueue.current!){ (data, error) in
        if let myData = data{
            let x = myData.acceleration.x
            let y = myData.acceleration.y
            let z = myData.acceleration.z
            print("Acceleration X: \(x) Y: \(y) Z: \(z)")
        }
    }
}

推荐答案

创建CSV文件所需要做的就是创建一个字符串,并用逗号分隔各列,并用换行符分隔各行,然后将该字符串写入文件

All you have to do to create a CSV file is create a string with commas separating your columns, and newlines separating your rows, then write the string to a file.

let csvString = allData
    .map { "\($0.x),\($0.y),\($0.z)" }
    .joined(separator: "\n")
csvString.write(toFile: "some-file.csv", atomically: true, encoding: .utf8)

这篇关于如何记录传感器数据并导出为CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆