我曾经尝试过了解如何在Matlab中导入文本文件数据(时间信号),可以帮帮忙吗?.txt文件的结构如下所示:
0,0006 0,0835 0,00130,00160,0019 0,0082 0,0026 -0,0193 0,0032 0,1115 0,0039 -0,1169 0,0045 0,0461 0,0052 -0,1185 0,0058 0,0048 0,0065 0,0087 0,0071 -0,1163 0,0078 0,09130,0084 0,0220,0091 0,0072 0,0097 -0,0829
在原始文件中,它看起来像是由制表符空格分隔的列:

发布于 2021-10-03 10:20:26
我有一个解决方案:
A = importdata ('text_file_neme.txt');
M = A(:,2);发布于 2021-10-02 18:12:22
只需将逗号替换为点并使用dlmread函数:
cat signal.txt
0.0006 0.0835
0.0013 0.0016
0.0019 0.0082
0.0026 -0.0193
0.0032 0.1115
0.0039 -0.1169
0.0045 0.0461
0.0052 -0.1185
0.0058 0.0048
0.0065 0.0087
0.0071 -0.1163
0.0078 0.0913
0.0084 0.022
0.0091 0.0072
0.0097 -0.0829format long
data=dlmread('signal.txt',' ',0,0)
data =
5.999999999999999e-04 8.350000000000000e-02
1.300000000000000e-03 1.600000000000000e-03
1.900000000000000e-03 8.200000000000001e-03
2.600000000000000e-03 -1.930000000000000e-02
3.200000000000000e-03 1.115000000000000e-01
3.900000000000000e-03 -1.169000000000000e-01
4.500000000000000e-03 4.610000000000000e-02
5.200000000000000e-03 -1.185000000000000e-01
5.800000000000000e-03 4.800000000000000e-03
6.500000000000000e-03 8.699999999999999e-03
7.100000000000000e-03 -1.163000000000000e-01
7.800000000000000e-03 9.130000000000001e-02
8.399999999999999e-03 2.200000000000000e-02
9.100000000000000e-03 7.200000000000000e-03
9.700000000000000e-03 -8.290000000000000e-02免责声明:我的电脑上没有MATLAB,所以我使用了Octave。根据我以前的经验,它应该也能在MATLAB中工作。
发布于 2021-10-03 03:19:43
这可以通过以下三个步骤完成:
file_string = fileread('my_file.txt'); % Read file contents to string.
file_string = strrep(file_string, ',', '.'); % Replace commas in string to periods.
file_mat = str2num(file_string); % Convert contents to a matrix.原来的问题是列用制表符分隔,图中的行用换行符分隔,但在文本中,行也用制表符分隔。如果文件使用换行符分隔行,则结果file_mat将被格式化为行和列。如果使用制表符分隔所有元素(列和行),那么file_mat将只包含一个向量。如果是这种情况,则可以使用以下命令重塑file_mat:
file_mat = reshape(file_mat, [2 length(file_mat)/2])'; % Convert vector to array with two columns.如下所示进行了验证:

https://stackoverflow.com/questions/69415012
复制相似问题