如何从两个文本文件中读取数据并将它们合并为一个矩阵?

时间:2019-02-14 09:49:30

标签: octave

我正在处理两个文本文件“ sample01.txt”(https://onedrive.live.com/?cid=10B030792EBE268A&id=10B030792EBE268A%21903&parId=root&o=OneUp)和sample02.txt(https://onedrive.live.com/?cid=10B030792EBE268A&id=10B030792EBE268A%21904&parId=root&o=OneUp)。 在第一个“ sample01.txt”中,有两列。一个用于T [K],另一个用于物种的摩尔浓度。在“ sample01.txt”中,垂直堆积3种(NC7H16,O2和CO)的数据。 在另一个文件“ sample02.txt”上,所有列并排排列,仅一个T [K]列对所有种类都通用。 在图像文件“ Composed_matrix_sample01_sample02.png”(https://onedrive.live.com/?cid=10B030792EBE268A&id=10B030792EBE268A%21906&parId=root&o=OneUp)中,我展示了如何合并两个文件中的数据。例如,如果有三个种类(在“ sample01.txt”中),我想拥有三个矩阵,就像我在“ Composed_matrix_sample01_sample02.png”中提供的样品一样。

到目前为止,我从目录中读取了两个文件,并确定了需要在“ sample01.txt”中垂直堆放数据的行号。但是基于这些行号,我不知道如何拆分长列并提取每种物种的数据(T [K]和摩尔浓度)。

% full path to files (I prefer working with full paths but you could    just use filenames)
clear all
clc
s1 =  fullfile('/home/ali/Desktop/test_JSR', 'sample01.txt'); 
s2 =  fullfile('/home/ali/Desktop/test_JSR', 'sample02.txt'); 
% Read sample02 (this one's simple)
s2Table = dlmread(s2,'',1,0);  
% 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 = strcmp(s1str(:,1), key);  %logical index identifying header rows

我想将“ sample01.txt”中的T [k]和摩尔浓度列与“ sample02.txt”中的T [k]和摩尔浓度列合并为相应的物种。并为每个物种创建类似于“ Composed_matrix_sample01_sample02.png”的矩阵。在这种情况下,存在3种(NC7H16,O2和CO),因此我想要一个循环,一个接一个地提供所需的合并矩阵。

N.B:01。两个文本文件中的物种编号将始终相同。但是,sample01.txt和sample02.txt中种类的列长度可能会有所不同,我希望用“ 911911911”填充空白单元格,稍后再将其删除。  02.物种数量不是固定的,因此需要一种通用的方法来覆盖任何数量的物种。

0 个答案:

没有答案
相关问题