在 Matlab 中导入文本文件信号数据
Import text file signal data in Matlab
试图了解如何在 Matlab 中导入文本文件数据(时间信号),请你帮忙。 .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,0829
在原始文件中,列似乎由制表符分隔 space:
只需用点替换逗号并使用 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.0829
format 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中应该也可以。
这可以通过三个步骤完成:
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.
验证如下:
我有解决办法:
A = importdata ('text_file_neme.txt');
M = A(:,2);
试图了解如何在 Matlab 中导入文本文件数据(时间信号),请你帮忙。 .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,0829
在原始文件中,列似乎由制表符分隔 space:
只需用点替换逗号并使用 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.0829
format 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中应该也可以。
这可以通过三个步骤完成:
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.
验证如下:
我有解决办法:
A = importdata ('text_file_neme.txt');
M = A(:,2);