How to read specific lines from a text file and store them in a matrix?
2 ビュー (過去 30 日間)
古いコメントを表示
Hello everyone,
I want to create a matrix from the attached text file that only includes the lines after the one that reads "Interval: 0.02 sec". Basically, I want a matrix that includes the headers of the columns and all the numeric data from the file, but none of the information preceding it.
Also, would it be possible to use the same code when reading information from a file with a different number or rows and columns?
Thank you!
0 件のコメント
採用された回答
Stephen23
2019 年 6 月 12 日
編集済み: Stephen23
2019 年 6 月 12 日
This code should work regardless of the number of rows or columns:
opt = {'Headerlines',3, 'CollectOutput',true};
fid = fopen('Example.txt','rt');
tgt = '';
while ~strncmpi(tgt,'Target',6)
tgt = fgetl(fid);
end
itm = textscan(fid,'Item%d%s','Delimiter','=');
fmt = repmat('%f',1,1+numel(itm{1}));
dat = textscan(fid,fmt,opt{:});
fclose(fid);
dat = dat{1};
giving:
>> size(dat)
ans =
12111 17
>> dat
dat =
12110 -66.174 -69.167 -77.607 -74.388 -66.369 -69.19 -77.367 -73.986 -2.6341 -0.046144 0.34017 -0.7492 -1.97 -2.3501 -2.3501 -2.3501
12109 -66.174 -69.167 -77.607 -74.388 -66.369 -69.19 -77.367 -73.986 -2.6341 -0.046144 0.34017 -0.7492 -1.97 -2.3501 -2.3501 -2.3501
12108 -68.547 -72.838 -79.49 -77.035 -68.078 -72.151 -78.954 -75.901 -2.8352 -0.24927 -1.2125 -4.7041 -1.97 -1.97 -2.3501 -2.3501
12107 -68.547 -72.838 -79.49 -77.035 -68.078 -72.151 -78.954 -75.901 -2.8352 -0.24927 -1.2125 -4.7041 -1.97 -1.97 -2.3501 -2.3501
12106 -71.372 -75.556 -80.754 -79.204 -70.49 -74.884 -80.715 -78.217 -3.1016 -3.7407 -1.6364 -2.9571 -1.97 -1.97 -1.97 -1.97
12105 -71.372 -75.556 -80.754 -79.204 -70.49 -74.884 -80.715 -78.217 -3.1016 -3.7407 -1.6364 -2.9571 -1.97 -1.97 -1.97 -1.97
12104 -73.434 -76.856 -83.268 -80.121 -72.997 -76.536 -82.741 -80.363 -3.6008 -0.69239 -1.8817 -4.322 -1.97 -1.97 -1.97 -1.97
12103 -73.434 -76.856 -83.268 -80.121 -72.997 -76.536 -82.741 -80.363 -3.6008 -0.69239 -1.8817 -4.322 -1.97 -1.97 -1.97 -1.97
12102 -75.472 -78.823 -85.593 -82.589 -74.971 -78.008 -84.924 -82.487 0.42387 -1.7753 -2.9945 -1.1647 -1.97 -1.97 -1.97 -1.97
12101 -75.472 -78.823 -85.593 -82.589 -74.971 -78.008 -84.924 -82.487 0.42387 -1.7753 -2.9945 -1.1647 -1.97 -1.97 -1.97 -1.97
... lots more rows here
6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
And some of the header information:
>> tgt
tgt =
Target: 0,31
>> itm{1}
ans =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>> itm{2}
ans =
'0,31:1.03 SPEED MEASURED 1 [rpm]'
'0,32:1.03 SPEED MEASURED 1 [rpm]'
'0,33:1.03 SPEED MEASURED 1 [rpm]'
'0,34:1.03 SPEED MEASURED 1 [rpm]'
'0,31:1.04 MOTOR SPEED [rpm]'
'0,32:1.04 MOTOR SPEED [rpm]'
'0,33:1.04 MOTOR SPEED [rpm]'
'0,34:1.04 MOTOR SPEED [rpm]'
'0,31:1.08 MOTOR TORQUE [%]'
'0,32:1.08 MOTOR TORQUE [%]'
'0,33:1.08 MOTOR TORQUE [%]'
'0,34:1.08 MOTOR TORQUE [%]'
'0,31:25.04 TORQUE REF B [%]'
'0,32:25.04 TORQUE REF B [%]'
'0,33:25.04 TORQUE REF B [%]'
'0,34:25.04 TORQUE REF B [%]'
7 件のコメント
Stephen23
2019 年 6 月 13 日
編集済み: Stephen23
2019 年 6 月 13 日
"...how exactly I can generate the headers from the cell array itm's contents"
First read the documentation for array2table where it specifies how to specify the table's "variable" names:
Then read the specification of valid variable names:
Are the contents of itm valid variable names? No, the headers contain many invalid characters, and are also not unique. So you will need to remove/replace those invalid characters, and make them unique. You could use any string manipulation tools/functions of your choice, e.g. regexprep, strrep, genvarname, etc., whatever you prefer. Read the documentation and experiment!
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!