Have tried to understand how to import text file data (time signal) in Matlab, can you please help. Structure of .txt file looks like this:
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
in original file it looks like to column separated by tab space:
CodePudding user response:
Just replace commas by dots and use dlmread function:
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
Disclaimer: I don't have MATLAB on my computer and therefore used Octave. Based on my previous experience, it should work in MATLAB as well.
CodePudding user response:
This can be done using three steps:
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.
The original questions states that the columns are separated by tab and it appears that the rows are separated by a line feed in the figure, but in the text, rows are also separated by tabs. If the file uses line feeds to separate rows, then the result file_mat
will already be formatted into rows and columns. If tabs are used to separate all elements (columns and rows), then file_mat
will contain a vector only. If this is the case, then file_mat
can be reshaped using the following:
file_mat = reshape(file_mat, [2 length(file_mat)/2])'; % Convert vector to array with two columns.