如何将文本+数字TSV读取到MATLAB中? [英] How to read a text+numeric TSV into MATLAB?

查看:909
本文介绍了如何将文本+数字TSV读取到MATLAB中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很简单,但是我在MATLAB文档中找不到答案.我有一个由约60,000行和38列组成的TSV文件.第一行具有列的名称,第一列完全由字母数字字符串组成.其余条目都是浮点数(尽管在很多情况下,该值仅表示为0,没有明确的小数点).

This should be trivial, but I can't find the answer in the MATLAB documentation. I have a TSV file consisting of ~60,000 rows and 38 columns. The first row has the names of the columns, and the first column consists entirely of alphanumeric strings. The remaining entries are all floating-point numbers (although, in many cases the value is expressed as just 0, without an explicit decimal point).

如何将这些数据读入MATLAB?

How can I read this data into MATLAB?

推荐答案

MATLAB为此提供了函数textscan.第二个参数是格式说明符.在您的情况下,第一个字段是字符串,因此我们使用%s,其余字段是浮点值,因此我们使用%f.您需要%f的37次重复-比本示例中的重复次数多一些:

MATLAB provides a function textscan for this purpose. The second argument is the format specifier. In your case, the first field is a string, so we use %s, the remaining fields are float values, so we use %f. You will need 37 repeats of %f - a few more than in this example:

fid = fopen('yourfile.tsv');
C = textscan(fid, '%s %f %f %f %f %f %f', 'HeaderLines', 1);
fclose(fid);

HeaderLines告诉textscan在开始之前要跳过多少行.您提到文件的第一行包含列名,因此我们跳过1行.

HeaderLines tells textscan how many lines to skip before starting. You mention that the first line in your file contains the column names, so we skip 1 line.

这篇关于如何将文本+数字TSV读取到MATLAB中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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