如何在 MATLAB 中从文本文件创建矩阵?

发布于 2024-07-24 22:14:06 字数 271 浏览 8 评论 0原文

我有一个包含 4 列的文本文件,每列有 65536 个数据点。 行中的每个元素都用逗号分隔。 例如:

X,Y,Z,AU
4010.0,3210.0,-440.0,0.0
4010.0,3210.0,-420.0,0.0
etc.

所以,我有 65536 行,每行有 4 个数据值,如上所示。 我想把它转换成矩阵。 我尝试将数据从文本文件导入到Excel文件中,因为这样很容易创建矩阵,但我丢失了一半以上的数据。

I have a text file which has 4 columns, each column having 65536 data points. Every element in the row is separated by a comma. For example:

X,Y,Z,AU
4010.0,3210.0,-440.0,0.0
4010.0,3210.0,-420.0,0.0
etc.

So, I have 65536 rows, each row having 4 data values as shown above. I want to convert it into a matrix. I tried importing data from the text file to an excel file, because that way its easy to create a matrix, but I lost more than half the data.

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

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

发布评论

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

评论(5

青芜 2024-07-31 22:14:06

如果文件中的所有条目都是数字,则只需使用 a = load('file.txt')。 它应该创建一个 65536x4 矩阵 a。 它甚至比 csvread 更容易

If all the entries in your file are numeric, you can simply use a = load('file.txt'). It should create a 65536x4 matrix a. It is even easier than csvread

陈年往事 2024-07-31 22:14:06

您是否尝试过使用“导入数据”?
您只需要文件名和分隔符的参数。

>> tmp_data = importdata('your_file.txt',',')

tmp_data = 

          data: [2x4 double]
      textdata: {'X'  'Y'  'Z'  'AU'}
    colheaders: {'X'  'Y'  'Z'  'AU'}


>> tmp_data.data

ans =

        4010        3210        -440           0
        4010        3210        -420           0

>> tmp_data.textdata

ans = 

    'X'    'Y'    'Z'    'AU'

Have you ever tried using 'importdata'?
The parameters you need only file name and delimiter.

>> tmp_data = importdata('your_file.txt',',')

tmp_data = 

          data: [2x4 double]
      textdata: {'X'  'Y'  'Z'  'AU'}
    colheaders: {'X'  'Y'  'Z'  'AU'}


>> tmp_data.data

ans =

        4010        3210        -440           0
        4010        3210        -420           0

>> tmp_data.textdata

ans = 

    'X'    'Y'    'Z'    'AU'
迷途知返 2024-07-31 22:14:06

您应该能够将文本文件直接读入 MATLAB(使用函数 FOPENFGETLFSCANFFCLOSE):

fid = fopen('file.dat','rt');  %# Open the data file
headerChars = fgetl(fid);      %# Read the first line of characters
data = fscanf(fid,'%f,%f,%f,%f',[4 inf]).';  %'# Read the data into a
                                              %# 65536-by-4 matrix
fclose(fid);  %# Close the data file

Instead of messing with Excel, you should be able to read the text file directly into MATLAB (using the functions FOPEN, FGETL, FSCANF, and FCLOSE):

fid = fopen('file.dat','rt');  %# Open the data file
headerChars = fgetl(fid);      %# Read the first line of characters
data = fscanf(fid,'%f,%f,%f,%f',[4 inf]).';  %'# Read the data into a
                                              %# 65536-by-4 matrix
fclose(fid);  %# Close the data file
伴我老 2024-07-31 22:14:06

最简单的方法是使用 MATLAB 的 csvread 函数。

还有工具可以读取 CSV 文件。

您也可以自己完成,没有太大困难:只需循环文件中的每一行,然后用逗号将其分割,然后将其放入数组中。

The easiest way to do it would be to use MATLAB's csvread function.

There is also this tool which reads CSV files.

You could do it yourself without too much difficulty either: Just loop over each line in the file and split it on commas and put it in your array.

泅渡 2024-07-31 22:14:06

建议您熟悉 dlmreadtextscan

dlmread 类似于 csvread,但因为它可以处理任何分隔符(制表符、空格等),所以我倾向于使用它而不是 csvread

textscan 是真正的主力:有很多选项,+它适用于打开的文件,并且对于处理“坏”输入(例如文件中的非数字数据)更加稳健。 它可以像 gnovice 的建议中的 fscanf 一样使用,但我认为它更快(不过不要引用我的话)。

Suggest you familiarize yourself with dlmread and textscan.

dlmread is like csvread but because it can handle any delimiter (tab, space, etc), I tend to use it rather than csvread.

textscan is the real workhorse: lots of options, + it works on open files and is a little more robust to handling "bad" input (e.g. non-numeric data in the file). It can be used like fscanf in gnovice's suggestion, but I think it is faster (don't quote me on that though).

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