将具有多行标题信息的大型 csv 数据文件读取到 Matlab 中

发布于 2024-08-30 21:46:00 字数 417 浏览 3 评论 0原文

有人对如何将逗号分隔的数据文件读入Matlab有什么建议吗?简单的解决方案(如 dlmread、fscanf)似乎不起作用,因为有多(10)行标题信息。我得到的最接近的解决方案是:

C=textscan(datafile)
G=cell2mat(C{1,1}(34:endoffile)}) //34 is the line the data starts
V=str2num(G)

这里的问题是数据例如如下所示:

;1.0345,937,18,763
;1.0355,947,4,652
etc.

当转换为矩阵时,单元格中的所有字符串必须具有相同的大小,否则会给出使用“vertcat”的错误。如果没有其他选择,我可以删除记事本中的标题,但是对于很多文件来说,这将是一项乏味的工作。

Does anyone have some advice how to read a comma separated data file into Matlab? The simple solutions (like dlmread, fscanf) do not seem to work, as there are multiple (10) lines of header information. The closest I got to a solution is:

C=textscan(datafile)
G=cell2mat(C{1,1}(34:endoffile)}) //34 is the line the data starts
V=str2num(G)

the problem here is that the data for instance looks like this:

;1.0345,937,18,763
;1.0355,947,4,652
etc.

When converting into the matrix all strings in the cell have to be of the same size, otherwise an error using 'vertcat' is given. If no other option, I could just delete the header in lets say notepad, but with many many files this would be a tedious job.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赢得她心 2024-09-06 21:46:00

DLMREAD 接受起始行/列参数,或者范围参数。因此,如果您的数据从第 10 行开始,您可以尝试

V = dlmread(datafile, '', 9, 0);

如果您愿意 TEXTSCAN,您可以指定要跳过的多个HeaderLines

V = textscan(datafile, ..., 'HeaderLines', 10, ...);

向下扫描到文档页面上的“用户可配置选项”以了解更多详细信息。

DLMREAD accepts starting row/column parameters, or alternatively a range parameter. So if your data starts on line 10, you could try

V = dlmread(datafile, '', 9, 0);

If you prefer TEXTSCAN, you can specify a number of HeaderLines to skip:

V = textscan(datafile, ..., 'HeaderLines', 10, ...);

Scan down to "User Configurable Options" on the documentation page for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文