【问题标题】:Import csv as MATLAB struct将 csv 导入为 MATLAB 结构
【发布时间】:2021-06-25 07:47:27
【问题描述】:

我有一个大的 csv 日志文件。这是一个简化的示例:

ts,a.b.c,a.b.d,a.b.e,b.c,b.d,c.d.e,c.d.f,c.g
2021-03-29 06:38:39,1.0000,2,3,28.20,1,2,3,4
2021-03-29 06:38:40,1.0000,2,3,28.20,1,2,3,0.000000

我正在使用 MATLAB 的导入数据工具来导入它,但不幸的是,它会从标题中删除所有点并将所有变量导入为,例如:abc、abd、abe 等。

将像上面这样的 csv 作为结构导入的有效方法是什么? 它正在寻找一种将数据导入为结构的方法:a、b 和 c 用于这个特定的日志文件,以便我可以轻松地以 a.b.c 或 c.d.f 的形式访问变量。

【问题讨论】:

  • 写一个映射函数[ts, a, b, c] = readData()

标签: matlab csv file import


【解决方案1】:

这是我想出的,只需使用 readtable。

function res = log_import(logfile)

log_table = readtable(logfile);

res = [];

for i = 1:width(log_table)
    str_path = log_table.Properties.VariableDescriptions{i};
    fields  = strsplit(str_path,'.');  
    res = setfield(res, fields{1:end}, log_table{:, i});
end

end

【讨论】:

    猜你喜欢
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    相关资源
    最近更新 更多