How to read data from a text file based on the title?
3 ビュー (過去 30 日間)
古いコメントを表示
I have two text files. First one is "Sample01.txt". Which is like this..
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203772/image.png)
And the second one is "Sample02.txt". Which is like this..
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203773/image.png)
How can i read data for T[K] and NC7H16_molef[-] and so for O2 and CO in both the text files by reading their title?
Actually i am trying to merge the corresponding data from two text files into one matrix. So there will be three matrix for NC7H16, O2 and CO.
For example for NC7H16 the desired matrix is like this ..
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203774/image.png)
N.B. The row number for NC7H16_molef[-], O2 and CO are not fixed.
4 件のコメント
Adam Danz
2019 年 2 月 12 日
The "Sample01.txt" in your image looks like an [n x 2] matrix but the one you attached is [m x 4]. Could you clarify which one is correct? Also, your "Sample02.txt" attachment is png, not txt, so it's not readable.
採用された回答
Adam Danz
2019 年 2 月 12 日
編集済み: Adam Danz
2019 年 2 月 12 日
Instead of selectively reading a chosen subset of your text files (which I'm not even sure is possible), you can read in the entire text file and then pull out the columns of data you're interested in working with.
Your Sample02.txt file is easy to read using readtable(). The Sample01.txt file, however, contains multiple sub-tables which made it more difficult to work with. I used textscan() to read in the entire file and then split up the sub-tables.
Once both files are read and cleaned up, you can split and combine the data very easily to create new matrices. My example produces the matrix you described in your question. Note that because the data from one file has less rows than the other file, zeros were added as padding on the tail end of the data.
A second version of this code uses header names and is in the comments below.
% full path to files (I prefer working with full paths but you could just use filenames)
s1 = fullfile('C:\Users\janedoe\Documents\MATLAB\', 'sample01.txt');
s2 = fullfile('C:\Users\janedoe\Documents\MATLAB\', 'sample02.txt');
% Read sample02 (this one's simple)
s2Table = readtable(s2);
% Read sample01; it will be read in as a cell array of strings
fid = fopen(s1);
s1str = textscan(fid, '%s %s', 'HeaderLines', 1);
fclose(fid);
s1str = [s1str{:}];
% Now separate each sub-table into it's own table
key = 'T[K]'; %the start of each row that identifies a new sub-table
headerIdx = contains(s1str(:,1), key); %logical index identifying header rows
s1cell = splitapply(@(x){x}, s1str, cumsum(headerIdx));
s1table = cellfun(@(x)array2table(str2double(x(2:end, :))), s1cell, 'UniformOutput', false);
% before combining data, tables need to have the same number of rows.
% Here we pad the shorter table with trailing zeros.
padRowsNeeded = size(s2Table,1) - cellfun(@(x)size(x,1), s1table);
s1Table = cellfun(@(x,y)[x;array2table(zeros(y,size(x,2)))], s1table, num2cell(padRowsNeeded),'UniformOutput', false);
% Now you can join columns to create a new array
% Here is an example
newMatrix = [s1Table{1}.Var1, s1Table{1}.Var2, s2Table.Var1, s2Table.Var2];
% Convert back to a table if you want to
% Note that the variable names can be pulled directly from the source files but will required some cleaning
% since some of them are invalid variable names.
newTable = array2table(newMatrix, 'VariableNames', {'TK_samp01', 'NC7H16_samp1', 'TK_samp2', 'NC7H16_samp2'})
And the result
newTable =
21×4 table
TK_samp01 NC7H16_samp1 TK_samp2 NC7H16_samp2
_________ ____________ ________ ____________
582 0.000621 576 0.00067913
600 0.000437 598 0.00044327
619 0.000294 619 0.00040821
639 0.000245 635 0.00042462
658 0.000247 651 0.00046359
680 0.000289 669 0.00052614
699 0.000322 690 0.00061422
720 0.000266 711 0.0007039
739 0.000301 729 0.0007637
768 0.000373 751 0.00078045
797 0.000174 783 0.00054059
830 3.54e-05 821 0.0001833
860 6.3e-06 860 7.9079e-05
880 3.17e-07 894 4.7073e-05
909 8.94e-08 931 3.1658e-05
0 0 969 2.3408e-05
0 0 1008 1.739e-05
0 0 1036 1.3428e-05
0 0 1074 8.4148e-06
0 0 1112 4.605e-06
0 0 1150 2.3107e-06
9 件のコメント
Adam Danz
2019 年 2 月 14 日
As the tables are created, they need to be stored somewhere. I choose to store them a cell array.
There are ways to later pull them from the cell array. It depends what you're doing with them.
If you explain how you plan to use the tables i can give you advice.
i didn't understand your last sentence.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Low-Level File I/O についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!